我一直在阅读Beej's Guide to Network Programming,并尝试使用带有std=c11gcc标志来编译前几个示例程序(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仅在此意义上是相关的,因为它排除了某些OS特定于包含文件的目录。尝试使用C11变体以及操作系统和gcc特定扩展名的-std=gnu11

关于c - C11网络编程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28862975/

10-11 21:22