gpt4 book ai didi

c++ - 对 imp_getaddrinfo 的 undefined reference

转载 作者:搜寻专家 更新时间:2023-10-30 23:58:18 24 4
gpt4 key购买 nike

我一直在学习如何通过 C++ 在 Windows 中进行网络编程,进展顺利,除了我遇到了一个问题,我现在大约 3-4 天都无法解决,我正在使用Dev-C++ 5.5.3(Orwell),编译器为TDM-GCC 4.7.1。我在链接器参数中添加了 "-lwsock32"(不带引号)。一切正常除了 “freeaddrinfo”“getaddrinfo”,下面是其中两个函数的内容。

undefined reference to `imp_getaddrinfo@16'

undefined reference to `imp_freeaddrinfo@4'

我在某处读到它要求我定义我想要的 Windows 版本使用,所以我根据需要定义了_WINNT_WIN32 0x0601,但没有用。这是我的代码(缩写):

#define _WINNT_WIN32 0x0601
#include <ws2tcpip.h>
#include <winsock2.h>
#include <stdio.h>

#define DEFAULT_PORT "27015"
// server

int main()
{
WSADATA wsaData;
ZeroMemory(&wsaData,sizeof(wsaData));
int nResult = WSAStartup(MAKEWORD(2,2),&wsaData);
if(nResult != 0)
{
printf(TEXT("WSAStartup function failed, value: %d\n"),nResult);
Sleep(5000);
return 0001;
}
else
printf(TEXT("WSAStartup function has succeeded! value: %d\n"),nResult);
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
nResult = getaddrinfo(NULL,DEFAULT_PORT,&hints,&result);
if(nResult != 0)
{
printf("getaddrinfo did not return 0... failure...");
WSACleanup();
return 0002;
}
SOCKET ListenSocket = INVALID_SOCKET;
ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
printf("Error at socket():%ld\n",WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 0003;
}
nResult = bind(ListenSocket, result->ai_addr,(int)result->ai_addrlen);
if(nResult == SOCKET_ERROR)
{
printf("bindfailed with error %d\n",WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 0004;
}
freeaddrinfo(result);
if(listen(ListenSocket,SOMAXCONN) == SOCKET_ERROR)
{
printf("lISTEN FAILED WITH ERROR %LD\n",WSAGetLastError());
closesocket(ListenSocket);
}
return 1337^9001;
}

谢谢。

最佳答案

确保在预处理器定义中定义了 _WIN32_WINNT,并使用 -lWs2_32 see here

关于c++ - 对 imp_getaddrinfo 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com