简体   繁体   English

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

[英]Why can't getaddrinfo be found when compiling with gcc and std=c99

I have the following code which I was trying to compile. 我有以下代码,我试图编译。 When I tried with std=c99 it failed with warnings about "implicit declaration of type struct addrinfo" and "implicit declaration of function getaddrinfo". 当我尝试使用std = c99时,它失败并显示有关“类型struct addrinfo的隐式声明”和“函数getaddrinfo的隐式声明”的警告。 It works with std=gnu99. 它适用于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;
}

The commands I used to compile is: 我用来编译的命令是:

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

Looking at fail.i (preprocessed header) I see that compiler is right: those types haven't been declared in the headers pulled in. 看看fail.i(预处理标题)我看到编译器是正确的:那些类型还没有在引入的头文件中声明。

So I went to the headers and noticed that getaddrinfo is surrounded by a guard #ifdef __USE_POSIX, which is obviously not declared when compiling with c99. 所以我去了标题并注意到getaddrinfo被一个保护#ifdef __USE_POSIX包围,显然在用c99编译时没有声明。

How do I tell gcc that I want to use c99 and POSIX? 我怎么告诉gcc我想用c99和POSIX? I don't really want to use gnu99 in case I decide to switch compilers later (eg Clang or icc). 如果我决定稍后切换编译器(例如Clang或icc),我真的不想使用gnu99。

Simply because getaddrinfo (POSIX.1g extension) is not part of the standard c99: 仅仅因为getaddrinfo (POSIX.1g扩展名)不是标准c99的一部分:

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

stay with -std=gnu99 or -D_POSIX_C_SOURCE=200112L 保持-std=gnu99-D_POSIX_C_SOURCE=200112L

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM