Chinaunix首页 | 论坛 | 博客
  • 博客访问: 612504
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-03-31 19:47:53

以下是我最近要用的一个函数,可是编译时出现了一个警告:

代码:
#include <stdio.h>
#include <unistd.h>
#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的前面,才能起作用。
代码:

#define  _GNU_SOURCE
#include
#include


int 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导出的代码中已经包含了正确的函数原型,警告就不会出现了。
 
阅读(2330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~