这个简单的 .c 文件:
#include <unistd.h>
void test() {
char string[40];
gethostname(string,40);
}
正常编译时,一切都很好:
$ cc -Wall -c -o tmp.o tmp.c
$
...但在使用C99模式编译时,会发出警告:
$ cc -Wall -std=c99 -c -o tmp.o tmp.c
tmp.c: In function `test':
tmp.c:5: warning: implicit declaration of function `gethostname'
$
生成的.o文件很好,链接也可以工作。我只是想摆脱这个警告。我可以通过在自己的.h文件中放置声明来以hacky的方式实现。
C99有什么特点导致unistd.h中的声明没有被包含进来?是否可以解决这个问题,而不失去C99的优势?
我在其他标准库中也看到了同样的问题。
#define _BSD_SOURCE
减10分。请使用#define _XOPEN_SOURCE 500
或者更好的方式在构建链中:cc -D_XOPEN_SOURCE=500 ...
。如果需要任何形式的可移植性,那么这是必须的。 - Dummy00001man gethostname
没有提到Feature Test Macro Requirements。有人知道为什么吗? - slim