当我编译以下代码时:
#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 之前,使其原型(prototype)化并已知返回 char *
。否则,gcc 将假定它返回默认类型 int
。除非定义了 _GNU_SOURCE ,否则看不到 stdlib.h 中的原型(prototype)。
以下内容符合要求,没有警告 -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/以准确了解该宏打开的程度以及它发生的变化。
关于realpath 函数的转换问题(C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1583873/