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

标签 c linux gcc warnings realpath

当我编译以下代码时:

#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/

相关文章:

python - 包含使用 python 访问 C 数据结构的二进制文件

c - 数组限制不起作用

c - 灰度图像转换 .img 到 .ras - 在 Windows 中的 Linux 灰度中白色和蓝色?! C

c - 如何制作#define 函数的循环?

c - 为什么 scanf() 在此代码中导致无限循环?

linux - 具有两个输入文件的 For 循环

linux - 一行 bash 脚本来编辑文件(不创建新文件)

gcc - 编译器如何跨平台(硬件)?

c++ - volatile 与内存栅栏

c - 为什么 scanf() 会导致此代码中的无限循环?