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

 

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导出的代码中已经包含了正确的函数原型,警告就不会出现了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值