当我编译以下代码时:
#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
这很简单。 Glibc 将 realpath() 视为 GNU 扩展,而不是 POSIX。因此,添加这一行:
#define _GNU_SOURCE
...在包含 stdlib.h 之前,使其原型化并已知返回char *
。否则,gcc 将假定它返回默认类型int
。 stdlib.h 中的原型是看不到的,除非_GNU_SOURCE
被定义为。
以下内容符合要求,没有通过 -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/ 以准确了解该宏打开的程度以及它发生的变化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)