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.
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.
Just #define
_WIN32_WINNT
before your includes.Supposedly the proper way to fix this is:
Or perhaps more sensibly adding
-DWINVER=WindowsXP
to yourCPPFLAGS
.Ref: http://mingw.5.n7.nabble.com/Undefined-reference-to-getaddrinfo-td5694.html
Note: didn’t work for me however.
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 usegetaddrinfo()
with some compilers (i.e. Watcom). It is better to wrap it in the same way as Windows SDK does: