Implicit declaration of function 'getaddrinfo' on MinGW

anonymous coward picture anonymous coward · Oct 7, 2012 · Viewed 8k times · Source

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 compile it (with MinGW gcc) I get the following warnings:

ext/socket/socket.c: In function 'sl_tcp_socket_init':
ext/socket/socket.c:98:5: warning implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
ext/socket/socket.c:104:9: warning implicit declaration of function 'freeaddrinfo' [-Wimplicit-function-declaration]

Then the entire thing fails to link with undefined references to getaddrinfo() and freeaddrinfo().

Now, according to this MSDN page, getaddrinfo() is supported on Windows and is located in the header file Ws2tcpip.h and the library file Ws2_32.lib.

I'm including Ws2tcpip.h and linking with -lWs2_32, so I'm not sure why this isn't working.

Answer

enhzflep picture enhzflep · Oct 7, 2012

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.