realpath 函数的转换问题(C 编程)

发布于 2024-08-07 21:49:57 字数 565 浏览 8 评论 0原文

当我编译以下代码时:

#define _POSIX_C_SOURCE 200112L
#define _ISOC99_SOURCE
#define __EXTENSIONS__

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>    

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);
}

我在包含对 realpath 函数的调用的行上收到一条警告,内容为:

warning: assignment makes pointer from integer without a cast

有人知道发生了什么吗?我运行的是 Ubuntu Linux 9.04

When I compile the following code:

#define _POSIX_C_SOURCE 200112L
#define _ISOC99_SOURCE
#define __EXTENSIONS__

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>    

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);
}

I get a warning on the line that contains the call to the realpath function, saying:

warning: assignment makes pointer from integer without a cast

Anybody know what's up? I'm running Ubuntu Linux 9.04

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-08-14 21:49:57

这很简单。 Glibc 将 realpath() 视为 GNU 扩展,而不是 POSIX。因此,添加以下行:

#define _GNU_SOURCE

... 在包含 stdlib.h 之前,以便它具有原型并已知返回 char *。否则,gcc 将假定它返回默认类型 int。除非定义了 _GNU_SOURCE ,否则看不到 stdlib.h 中的原型。

以下内容符合要求,没有通过 -Wall 发出警告:

#include <stdio.h>
#include <limits.h>

#define _GNU_SOURCE
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);

    return 0;
}

您将看到其他流行扩展(例如 asprintf())的类似行为。值得一看 /usr/include/ 以准确了解该宏打开的程度以及它发生的变化。

This is very simple. Glibc treats realpath() as a GNU extension, not POSIX. So, add this line:

#define _GNU_SOURCE

... prior to including stdlib.h so that it is prototyped and known to to return char *. Otherwise, gcc is going to assume it returns the default type of int. The prototype in stdlib.h is not seen unless _GNU_SOURCE is defined.

The following complies fine without warnings with -Wall passed:

#include <stdio.h>
#include <limits.h>

#define _GNU_SOURCE
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);

    return 0;
}

You will see similar behavior with other popular extensions such as asprintf(). Its worth a look at /usr/include/ to see exactly how much that macro turns on and what it changes.

无边思念无边月 2024-08-14 21:49:57

编译器不知道realpath是什么,因此它假设它是一个返回int的函数。它这样做是有历史原因的:许多旧的 C 程序都依赖它来这样做。

您可能错过了它的声明,例如忘记#include它的头文件。

The compiler doesn't know what realpath is, so it assumes it's a function returning int. It does this for historical reasons: a lot of older C programs relied on it doing this.

You're probably missing the declaration of it, e.g. by forgetting to #include its header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文