用实时路径功能投射问题(C编程) [英] casting issue with realpath function (c programming)
问题描述
当我编译以下代码时:
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
有人知道这是怎么回事?我正在运行Ubuntu Linux 9.04
Anybody know what's up? I'm running Ubuntu Linux 9.04
推荐答案
这很简单。 Glibc将realpath()视为GNU扩展,而不是POSIX。所以,添加这一行:
This is very simple. Glibc treats realpath() as a GNU extension, not POSIX. So, add this line:
#define _GNU_SOURCE
...在包含stdlib.h之前,这样它的原型和已知要返回 char *
。否则,gcc将假定它返回默认类型 int
。除非定义 _GNU_SOURCE
,否则不会看到stdlib.h中的原型。
... 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.
以下是符合要求的,没有警告,通过-Wall通过:
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;
}
您将看到与其他常用扩展名(如asprintf())类似的行为。它值得看一看/ usr / include /中的内容,看看这个宏究竟打开了多少,以及它发生了什么变化。
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.
这篇关于用实时路径功能投射问题(C编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!