Search code examples
ctcpnetwork-programminggetaddrinfo

How can I fix `getaddrinfo` definitions not being found?


I get the following errors when I try to compile my code:

main.c:13:40: note: each undeclared identifier is reported only once for each function it appears in
main.c:17:14: error: ‘EAI_ADDRFAMILY’ undeclared (first use in this function); did you mean ‘EAI_FAMILY’?
         case EAI_ADDRFAMILY:
              ^~~~~~~~~~~~~~
              EAI_FAMILY
main.c:29:14: error: ‘EAI_NODATA’ undeclared (first use in this function); did you mean ‘EAI_NONAME’?
         case EAI_NODATA:
              ^~~~~~~~~~
              EAI_NONAME

I've tried including different headers (e.g. netinet/in.h) as well as compiling with different standards (gnu90, gnu99, gnu11), but nothing seems to resolve the error.

The following is my code:

// Name: main.c
// Compile: gcc -std=gnu11 main.c
// Error:
//   main.c: In function ‘main’:
//   main.c:18:14: error: ‘EAI_ADDRFAMILY’ undeclared (first use in this function); did you mean ‘EAI_FAMILY’?
//            case EAI_ADDRFAMILY:
//                 ^~~~~~~~~~~~~~
//                 EAI_FAMILY
//   main.c:18:14: note: each undeclared identifier is reported only once for each function it appears in
//   main.c:30:14: error: ‘EAI_NODATA’ undeclared (first use in this function); did you mean ‘EAI_NONAME’?
//            case EAI_NODATA:
//                 ^~~~~~~~~~
//                 EAI_NONAME

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

int main()
{
    struct addrinfo* res;
    char* domain = "google.com";

    switch(getaddrinfo(domain, "http", NULL, &res))
    {
        case 0:
            break;
        case EAI_ADDRFAMILY:
            break;
        case EAI_AGAIN:
            break;
        case EAI_BADFLAGS:
            break;
        case EAI_FAIL:
            break;
        case EAI_FAMILY:
            break;
        case EAI_MEMORY:
            break;
        case EAI_NODATA:
            break;
        case EAI_NONAME:
            break;
        case EAI_SERVICE:
            break;
        case EAI_SOCKTYPE:
            break;
    }

    return 0;
}

Solution

  • It turned out that although the two definitions, EAI_ADDRFAMILY and EAI_NODATA, were defined in netdb.h on my system, they were not enabled because it required that __USE_GNU be defined.

    To enable __USE_GNU (indirectly), we define _GNU_SOURCE at the top of the file. The working file is the following:

    // Name: main.c
    // Compile: gcc -std=gnu11 main.c
    
    #define _GNU_SOURCE // Need for EAI_ADDRFAMILY and EAI_NODATA
    
    #include <stddef.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    int main()
    {
        struct addrinfo* res;
        char* domain = "google.com";
    
        switch(getaddrinfo(domain, "http", NULL, &res))
        {
            case 0:
                break;
            case EAI_ADDRFAMILY:
                break;
            case EAI_AGAIN:
                break;
            case EAI_BADFLAGS:
                break;
            case EAI_FAIL:
                break;
            case EAI_FAMILY:
                break;
            case EAI_MEMORY:
                break;
            case EAI_NODATA:
                break;
            case EAI_NONAME:
                break;
            case EAI_SERVICE:
                break;
            case EAI_SOCKTYPE:
                break;
        }
    
        return 0;
    }