我一直在阅读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 来做这些事情?
最佳答案
C11 仅在排除包含文件的某些操作系统特定目录的意义上才与此相关。尝试使用 -std=gnu11
这是 C11 变体加上操作系统和 gcc 特定扩展。
关于C11网络编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28862975/