当前位置: 首页 > 工具软件 > dir2cast > 使用案例 >

嵌入式Linux系统:应用开发基础_get_current_dir_name编译警告

詹杰
2023-12-01

 

get_current_dir_name编译警告

以下是我最近要用的一个函数,可是编译时出现了一个警告:
代码:
#include
#include
#define _GNU_SOURCE
int main(int argc,char *argv[])
{
        char *path;
        path=get_current_dir_name();
        printf("%s>\n",path);
        exit(0);
}
$gcc test.c
test.c: In function `main':
test.c:9: warning: assignment makes pointer from integer without a cast

查看过手册,里面是说
get_current_dir_name, which is only prototyped if _GNU_SOURCE is defined, will malloc(3) an array big enough to hold the current directory name. If the environment variable PWD is set, and its value is correct, then that value will be returned.
找到了友人的答案:
明白了,宏定义的位置应该放在unistd.h的前面,才能起作用。-----------放在所有#include的前面。


代码:
#define  _GNU_SOURCE
#include
#include
#define _GNU_SOURCEint main(int argc,char *argv[])
{
        char *path;
        path = get_current_dir_name();
        printf("%s>\n",path);
        exit(0);
}

用gcc -E将预处理的代码导出来查看,如果宏定义的位置不正确。
导出的代码中不会包含get_current_dir_name()的函数原型,自然编译就认为它的返回值是默认的整数,从而导致一个警告。
把宏定义放在前面之后,gcc -E导出的代码中已经包含了正确的函数原型,警告就不会出现了。

 类似资料: