2012-10-07 76 views
5

我有一個使用getaddrinfo()的C程序。它在Linux和Mac OS X上按預期工作。在MinGW上隱式聲明函數'getaddrinfo'

我正在將它移植到Windows中。

當我編譯它(使用MinGW GCC)我得到以下警告:

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] 

然後整個事情失敗,於getaddrinfo()freeaddrinfo()未定義的引用鏈接。現在

,根據this MSDN pagegetaddrinfo()支持Windows和位於頭文件Ws2tcpip.h和庫文件Ws2_32.lib

我包括Ws2tcpip.h和鏈接-lWs2_32,所以我不知道爲什麼這不起作用。

回答

10

如果你看看ws2tcpip.h的297線,你可以看到,有_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 

你只包括的#define _WIN32_WINNT之前。

0

如果你想使你的代碼編譯範圍,你應該居然還用相同的操作系統版本爲_WIN32_WINNT定義NTDDI_VERSION。沒有這個定義,只有_WIN32_WINNT不會讓你在某些編譯器(即Watcom)中使用getaddrinfo()。這是更好地與Windows SDK確實把它包起來以同樣的方式:

#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