admin管理员组文章数量:1612098
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导出的代码中已经包含了正确的函数原型,警告就不会出现了。
本文标签: 嵌入式基础系统getcurrentdirnameLinux
版权声明:本文标题:嵌入式Linux系统:应用开发基础_get_current_dir_name编译警告 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728628792a1166840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论