我已經收到這個錯誤很長時間了,Google也沒有太多幫助。對getaddrinfo的未定義引用
我是Winsock編程的新手,並試圖從在線資源學習。我正在嘗試使用MSDN網站上的詳細信息構建一個簡單的服務器。每當我編譯代碼(MinGW)時,就會看到標題(Undefined reference to getaddrinfo
)中提到的錯誤。下面是代碼:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define WINVER WindowsXP
#include <windows.h>
#include <winsock2.h>
#include <winsock.h>
#include <ws2tcpip.h>
#include <stdio.h>
int main() {
WSADATA wsaData;
int iResult;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
#define DEFAULT_PORT "27015"
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;
// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
return 0;
}
我用下面的命令編譯:
gcc msdn_np.c -o msdn_np.exe -lWS2_32
但我正在鏈接庫Ws2_32(請參閱OP中的第三行),但我仍然遇到問題,這是令人沮喪的。 – check123 2011-03-07 17:17:58
我猜想與核電廠和明戈合作非常棘手。我更改爲CodeBlocks,並將鏈接器設置更改爲包含Ws2_32.dll,現在其工作正常。謝謝! – check123 2011-03-08 17:19:00