为什么用gcc编译时没有的getaddrinfo被发现和性病= C99 [英] Why can't getaddrinfo be found when compiling with gcc and std=c99
问题描述
我有以下的code,我试图编译。当我与性病= C99试了一下有关式结构addrinfo中的隐式声明和函数的getaddrinfo的隐式声明的警告失败。它与STD = gnu99。
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". It works with 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
看着fail.i(preprocessed头)我看到的编译器是正确的:那些类型没有在头被宣布为拉
Looking at fail.i (preprocessed header) I see that compiler is right: those types haven't been declared in the headers pulled in.
于是我去了头,发现的getaddrinfo被保护的#ifdef __USE_POSIX,这与C99编译时显然没有声明包围。
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.
我如何告诉我要使用C99和POSIX GCC?我真的不希望使用gnu99的情况下,我决定以后改用编译器(如锵或ICC)。
How do I tell gcc that I want to use c99 and POSIX? I don't really want to use gnu99 in case I decide to switch compilers later (eg Clang or icc).
推荐答案
很简单,因为的getaddrinfo
(POSIX.1g扩展名)不是C99标准的一部分:
Simply because getaddrinfo
(POSIX.1g extension) is not part of the standard c99:
<一个href=\"http://www.schweikhardt.net/identifiers.html\">http://www.schweikhardt.net/identifiers.html
连住 -std = gnu99
或 -D_POSIX_C_SOURCE = 200112L
这篇关于为什么用gcc编译时没有的getaddrinfo被发现和性病= C99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!