我有下面的代码,我试图编译。当我尝试使用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?我真的不想使用gnu99,以防以后决定切换编译器(如clang或icc)。
最佳答案
仅仅因为getaddrinfo
(posix.1g扩展)不是标准c99的一部分:
http://www.schweikhardt.net/identifiers.html
保持-std=gnu99
或-D_POSIX_C_SOURCE=200112L
关于c - 使用gcc和std = c99进行编译时为什么找不到getaddrinfo,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12024703/