MinGW上函数'getaddrinfo'的隐式声明 [英] Implicit declaration of function 'getaddrinfo' on MinGW
问题描述
我有一个使用getaddrinfo()
的C程序.它可以在Linux和Mac OS X上正常运行.
I have a C program that uses getaddrinfo()
. It works as expected on Linux and Mac OS X.
我正在将其移植到Windows.
I'm in the middle of porting it to Windows.
当我使用MinGW gcc编译它时,收到以下警告:
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]
然后整个事情都无法链接到未定义的对getaddrinfo()
和freeaddrinfo()
的引用.
Then the entire thing fails to link with undefined references to getaddrinfo()
and freeaddrinfo()
.
现在,根据此MSDN页面,Windows上支持getaddrinfo()
,它位于头文件Ws2tcpip.h
和库文件Ws2_32.lib
中.
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
.
我包括了Ws2tcpip.h
并与-lWs2_32
链接,所以我不确定为什么它不起作用.
I'm including Ws2tcpip.h
and linking with -lWs2_32
, so I'm not sure why this isn't working.
推荐答案
如果看一下ws2tcpip.h的第297行,您会看到对_WIN32_WINNT的值进行了检查.
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
只需在包含之前#define _WIN32_WINNT
.
Just #define _WIN32_WINNT
before your includes.
这篇关于MinGW上函数'getaddrinfo'的隐式声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!