Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage Size of addrinfo isn't known

Tags:

c

networking

I am writing a simple network program in C. When I turned on -Wall with --std=c11, I got an error message about the way am declaring a struct.

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


int main() {

        struct addrinfo res;

    return 0;
}

The type addrinfo is defined in the sys/types.h file. I don't get an error when using a pointer.

How can I resolve this error message?

simple.c:9:25: error: storage size of ‘res’ isn’t known
         struct addrinfo res;
                         ^
like image 455
Leland Barton Avatar asked May 31 '16 09:05

Leland Barton


1 Answers

Several points:

  1. The type addrinfo is actually defined in netdb.h.
  2. You can use the -E flag to gcc to see the pre-processor output and discover that the addrinfo structure is actually not defined in your code. Now you should suspect that probably some definition is missing.
  3. As can be seen here, in order to expose the definition you need to use the _POSIX_C_SOURCE feature test macro.
  4. So, this should resolve the mentioned error message:

    #define _POSIX_C_SOURCE 200112L
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    int main() {
        struct addrinfo res;
    
        return 0;
    }
    
like image 84
s7amuser Avatar answered Oct 28 '22 10:10

s7amuser