-2

I've been reading Beej's Guide to Network Programming and tried to compile the first few example programs (section 5.1) using the std=c11 flag with gcc. I keep getting errors like these (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);

However, without the c11 flag it compiles fine. What is exactly causing this incompatibility? And should I even be using C11 for these kind of things?

John Bergson
  • 425
  • 1
  • 4
  • 7
  • There is nowhere near enough information to answer this question. How is `struct addrinfo` defined? Does it contain a construct which is treated differently in C11? the `incomplete type` messages are a fairly big hint. – kdopen Mar 04 '15 at 19:22
  • you need to show more code, we cannot answer it like this, I know the book you are reading, i read it as well, so you need to show more code – Gravity Mar 04 '15 at 19:27
  • Adding std=c11 undefines __USE_POSIX macro which guards addrinfo struct in netdb.h – jaroslawj Mar 04 '15 at 19:34

2 Answers2

1

C11 can only be relevant here in the sense that this excludes some OS specific directories for include files. Try to use -std=gnu11 which is the C11 variant plus OS and gcc specific extensions.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

Without -std GCC defaults to a GNU dialect of ISO C90 (C11 with the upcoming 5.0). This will make everything available. But with the -std=c11 option you're saying that you only want ISO C11 features. struct addrinfo and the related functions are POSIX features. The man page tells you how to make those available:

   getaddrinfo(), freeaddrinfo(), gai_strerror():
       _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

So define _POSIX_C_SOURCE as 1 before including the header. Either by using #define _POSIX_C_SOURCE 1 or -D_POSIX_C_SOURCE=1.

cremno
  • 4,672
  • 1
  • 16
  • 27