LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1080|回复: 5

关于用get_current_dir_name()时的一个警告

[复制链接]
发表于 2005-3-20 13:08:27 | 显示全部楼层 |阅读模式
以下是我最近要用的一个函数,可是编译时出现了一个警告:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #define  _GNU_SOURCE

  4. int main(int argc,char *argv[])
  5. {
  6.         char *path;

  7.         path=get_current_dir_name();
  8.         printf("%s>\n",path);
  9.         exit(0);
  10. }

  11. $gcc test.c

  12. test.c: In function `main':
  13. 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.

横竖我都看不出他凭什么给这个警告我!我知道是我的功力还不行啊!请大家帮忙分析一下!好去掉这个警告!(程序还是可以通过运行!)谢谢!
发表于 2005-3-20 20:02:59 | 显示全部楼层
很奇怪,这个函数的返回值居然是int。

  1. bash-2.05b$ gdb a.out
  2. GNU gdb 6.0
  3. Copyright 2003 Free Software Foundation, Inc.
  4. GDB is free software, covered by the GNU General Public License, and you are
  5. welcome to change it and/or distribute copies of it under certain conditions.
  6. Type "show copying" to see the conditions.
  7. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  8. This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

  9. (gdb) l
  10. 1       #include <stdio.h>
  11. 2       #include <unistd.h>
  12. 3       #define  _GNU_SOURCE
  13. 4       #define  __USE_GNU
  14. 5
  15. 6       int main(int argc,char *argv[])
  16. 7       {
  17. 8               char *path;
  18. 9               path = get_current_dir_name();
  19. 10              printf("%s>\n",path);
  20. (gdb) ptype path
  21. No symbol "path" in current context.
  22. (gdb) b 10
  23. Breakpoint 1 at 0x804840c: file tt.c, line 10.
  24. (gdb) r
  25. Starting program: /home/kj501/program/c/a.out
  26. warning: Unable to find dynamic linker breakpoint function.
  27. GDB will be unable to debug shared library initializers
  28. and track explicitly loaded dynamic code.

  29. Breakpoint 1, main (argc=1, argv=0xbffff3d4) at tt.c:10
  30. 10              printf("%s>\n",path);
  31. (gdb) ptype path
  32. type = char *
  33. (gdb) ptype  get_current_dir_name()
  34. type = int
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-3-20 20:08:48 | 显示全部楼层
如果把函数的返回值赋值给一个整数,警告确实会消失,但这与函数原型的声明不符。具体是什么原因,我也不清楚。
回复 支持 反对

使用道具 举报

发表于 2005-3-20 20:20:51 | 显示全部楼层
感觉好象是头文件的问题造成的。
因为函数如果不声明函数原型,默认的返回值就是整型。把unistd.h去掉后,得到的警告是一样的。
好象这个unistd.h没有起到应有的作用。
回复 支持 反对

使用道具 举报

发表于 2005-3-20 20:41:27 | 显示全部楼层
明白了,宏定义的位置应该放在unistd.h的前面,才能起作用。

  1. #define  _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <stdio.h>

  4. int main(int argc,char *argv[])
  5. {
  6.         char *path;

  7.         path = get_current_dir_name();
  8.         printf("%s>\n",path);
  9.         exit(0);
  10. }
复制代码

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

使用道具 举报

 楼主| 发表于 2005-3-21 10:08:17 | 显示全部楼层
Post by kj501
明白了,宏定义的位置应该放在unistd.h的前面,才能起作用。

  1. #define  _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <stdio.h>

  4. int main(int argc,char *argv[])
  5. {
  6.         char *path;

  7.         path = get_current_dir_name();
  8.         printf("%s>\n",path);
  9.         exit(0);
  10. }
复制代码

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


兄弟好强啊!
  问题解决了,也学到了东西!谢谢了!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表