Implicit declaration of function 'getaddrinfo' on MinGW

后端 未结 3 423
醉梦人生
醉梦人生 2020-12-17 02:43

I have a C program that uses getaddrinfo(). It works as expected on Linux and Mac OS X.

I\'m in the middle of porting it to Windows.

When I comp

相关标签:
3条回答
  • 2020-12-17 02:49

    Supposedly the proper way to fix this is:

    #define WINVER WindowsXP
    

    Or perhaps more sensibly adding -DWINVER=WindowsXP to your CPPFLAGS.

    Ref: http://mingw.5.n7.nabble.com/Undefined-reference-to-getaddrinfo-td5694.html

    Note: didn’t work for me however.

    0 讨论(0)
  • 2020-12-17 03:01

    If you want to make your code compiler-wide you should actually also define NTDDI_VERSION with the same OS version as _WIN32_WINNT. Without that defining only _WIN32_WINNT will not let you to use getaddrinfo() with some compilers (i.e. Watcom). It is better to wrap it in the same way as Windows SDK does:

    #define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)    ver##0000
    #define _NTDDI_VERSION_FROM_WIN32_WINNT(ver)     _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)
    
    #ifndef _WIN32_WINNT
    #  define _WIN32_WINNT 0x501
    #endif
    #ifndef NTDDI_VERSION
    #  define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)
    #endif
    
    0 讨论(0)
  • 2020-12-17 03:07

    If you have a look at line 297 of ws2tcpip.h, you can see that there's a check of the value of _WIN32_WINNT.

    #if (_WIN32_WINNT >= 0x0501)
    void WSAAPI freeaddrinfo (struct addrinfo*);
    int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
                    struct addrinfo**);
    int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
                   char*,DWORD,int);
    #else
    /* FIXME: Need WS protocol-independent API helpers.  */
    #endif
    

    Just #define _WIN32_WINNT before your includes.

    0 讨论(0)
提交回复
热议问题