本文介绍了的getaddrinfo()在Win32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个使用的getaddrinfo C程序()。它按预期工作在Linux和Mac OS X。

我在将它移植到Windows的中间。

当我编译它(使用MinGW GCC)我得到以下警告:

 转/插座/ socket.c中:在函数'sl_tcp_socket_init:
EXT /插座/ socket.c中:98:5:报警功能的getaddrinfo'的隐式声明[-Wimplicit功能声明]
EXT /插座/ socket.c中:104:9:报警功能freeaddrinfo'隐式声明[-Wimplicit功能声明]

然后整个事情失败,到的getaddrinfo未定义的引用() freeaddrinfo()

现在,根据,的getaddrinfo()支持Windows和位于头文件 Ws2tcpip.h 和图书馆文件 WS2_32.LIB

我包括 Ws2tcpip.h -lWs2_32 链接,所以我不知道为什么,这不工作。


解决方案

如果你看看ws2tcpip.h的297线,你可以看到,有_WIN32_WINNT的值的检查。

 #如果(_WIN32_WINNT> = 0x0501)
无效WSAAPI freeaddrinfo(结构addrinfo中*);
INT WSAAPI的getaddrinfo(为const char *,为const char *,const的结构addrinfo中的*
                结构addrinfo中**);
INT WSAAPI则getnameinfo(const的结构sockaddr *,socklen_t的,字符*,DWORD,
               字符*,DWORD,INT);
#其他
/ * FIXME:需要WS协议的API助手。 * /
#万一

刚的#define _WIN32_WINNT 您之前包括

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.

#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.

这篇关于的getaddrinfo()在Win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 08:19