![](/img/trans.png)
[英]Why does gcc not complain about htons() but complains about getaddrinfo() when compiled with -std=c99?
[英]Why can't getaddrinfo be found when compiling with gcc and std=c99
我有以下代码,我试图编译。 当我尝试使用std = c99时,它失败并显示有关“类型struct addrinfo的隐式声明”和“函数getaddrinfo的隐式声明”的警告。 它适用于std = gnu99。
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int fails(const char *host, const char *port, struct addrinfo *hints)
{
int rc;
struct addrinfo *results;
// can't find this function??
rc = getaddrinfo(host, port, hints, &results);
// free memory in this important application
freeaddrinfo(results);
return rc;
}
我用来编译的命令是:
gcc -c -o fail.o -Wall -Werror -std=c99 -save-temps fail.c
gcc -c -o fail.o -Wall -Werror -std=gnu99 -save-temps fail.c
看看fail.i(预处理标题)我看到编译器是正确的:那些类型还没有在引入的头文件中声明。
所以我去了标题并注意到getaddrinfo被一个保护#ifdef __USE_POSIX包围,显然在用c99编译时没有声明。
我怎么告诉gcc我想用c99和POSIX? 如果我决定稍后切换编译器(例如Clang或icc),我真的不想使用gnu99。
仅仅因为getaddrinfo
(POSIX.1g扩展名)不是标准c99的一部分:
http://www.schweikhardt.net/identifiers.html
保持-std=gnu99
或-D_POSIX_C_SOURCE=200112L
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.