realpath 函数的转换问题(C 编程)
当我编译以下代码时:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单。 Glibc 将 realpath() 视为 GNU 扩展,而不是 POSIX。因此,添加以下行:
... 在包含 stdlib.h 之前,以便它具有原型并已知返回
char *
。否则,gcc 将假定它返回默认类型int
。除非定义了 _GNU_SOURCE ,否则看不到 stdlib.h 中的原型。以下内容符合要求,没有通过 -Wall 发出警告:
您将看到其他流行扩展(例如 asprintf())的类似行为。值得一看 /usr/include/ 以准确了解该宏打开的程度以及它发生的变化。
This is very simple. Glibc treats realpath() as a GNU extension, not POSIX. So, add this line:
... 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 ofint
. The prototype in stdlib.h is not seen unless_GNU_SOURCE
is defined.The following complies fine without warnings with -Wall passed:
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.
编译器不知道
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.