我一直在阅读Beej's Guide to Network Programming并尝试使用std=c11
标记gcc
来编译前几个示例程序(section 5.1)。我一直收到这些错误(showip.c
):
showip.c: In function ‘main’:
showip.c:15:21: error: storage size of ‘hints’ isn’t known
struct addrinfo hints, *res, *p;
^
showip.c:35:33: error: dereferencing pointer to incomplete type
for(p = res;p != NULL; p = p->ai_next) {
^
showip.c:41:14: error: dereferencing pointer to incomplete type
if (p->ai_family == AF_INET) { // IPv4
^
showip.c:42:63: error: dereferencing pointer to incomplete type
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
^
showip.c:46:65: error: dereferencing pointer to incomplete type
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
^
showip.c:52:20: error: dereferencing pointer to incomplete type
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
然而,没有c11
标志,它编译得很好。究竟是什么导致了这种不兼容性?我是否应该将C11用于这类事情?
答案 0 :(得分:1)
C11只能与此相关,因为它排除了包含文件的某些操作系统特定目录。尝试使用-std=gnu11
,这是C11变体加上OS和gcc特定扩展。
答案 1 :(得分:0)
没有-std
GCC默认为ISO C90的GNU方言(C11和即将推出的5.0)。这将使一切都可用。但是使用-std=c11
选项,您只是说您只需要ISO C11功能。 struct addrinfo
和相关函数是POSIX功能。 The man page告诉您如何使这些可用:
getaddrinfo(), freeaddrinfo(), gai_strerror():
_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
因此,在包含标头之前,请将_POSIX_C_SOURCE
定义为1
。使用#define _POSIX_C_SOURCE 1
或-D_POSIX_C_SOURCE=1
。