[英]C error: undefined reference to
我在C(Windows)中使用庫Winsock創建了以下套接字服務器。
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "WS2_32.lib")
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
int __cdecl main(void) {
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
char recvbuf[DEFAULT_BUFLEN];
int iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
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;
}
// Create a SOCKET for the server to listen for client connections
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 1;
}
// Set up a TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result), // to free the memory allocated by getaddrinfo function
// Listen on a socket
iResult = listen(ListenSocket, SOMAXCONN);
if(iResult == SOCKET_ERROR) { // SOMAXCONN indicates the backlog value, maximum length of the queue of pending connections to accept
printf("Listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept connection on a socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if(ClientSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
closesocket(ListenSocket); // No longer need server socket
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if(iResult > 0) {
printf("Bytes received: %d\n", iResult);
// Echo buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if(iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
} else if(iResult == 0) {
printf("Connection closing...\n");
} else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while(iResult > 0);
// Shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ClientSocket, SD_SEND);
if(iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closessocket(ClientSocket);
WSACleanup();
return 1;
}
//cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
當我嘗試編譯它時,Winsock庫中的每個函數都會出現以下錯誤:
C:\Users\Victor\AppData\Local\Temp\ccOwFjRF.o:socketServer.c:(.text+0x4a): undef
ined reference to `WSAStartup@8'
我添加了Winsock庫所在的目錄,但似乎我需要做其他事情。 有沒有人對這個問題有任何想法? 謝謝。
為了使編譯成功,您需要執行以下更改:
在源文件中,您必須在包含標頭之前定義#define _WIN32_WINNT 0x0501
。 有關詳細信息,請參閱此前文章 。
消息來源中有一個拼寫錯誤。 請在第5行將closessocket
替換為closesocket
。 您的源文件的121。
使用以下命令編譯
gcc -o socketServer socketServer.c -lws2_32 -lwsock32 -L $MinGW\\lib
$MinGW
是MinGW softare的安裝目錄
通過這些更改,我能夠成功編譯您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.