我有以下我试图编译的代码。当我尝试使用 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 http://www.schweikhardt.net/identifiers.html
留下来-std=gnu99
or -D_POSIX_C_SOURCE=200112L
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)