为什么在使用gcc和std = c99进行编译时找不到getaddrinfo

dav*_*ave 17 c gcc posix

我有以下代码,我试图编译.当我尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

我用来编译的命令是:

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
Run Code Online (Sandbox Code Playgroud)

看看fail.i(预处理标题)我看到编译器是正确的:那些类型还没有在引入的头文件中声明.

所以我去了标题并注意到getaddrinfo被一个保护#ifdef __USE_POSIX包围,显然在用c99编译时没有声明.

我怎么告诉gcc我想用c99和POSIX?如果我决定稍后切换编译器(例如Clang或icc),我真的不想使用gnu99.

TOC*_*TOC 19

仅仅因为getaddrinfo(POSIX.1g扩展名)不是标准c99的一部分:

http://www.schweikhardt.net/identifiers.html

留在-std=gnu99-D_POSIX_C_SOURCE=200112L