【问题标题】:C error: undefined reference toC 错误:未定义的引用
【发布时间】:2015-01-20 22:47:23
【问题描述】:

我使用 Winsock 库在 C (Windows) 中创建了以下套接字服务器。

#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 库所在的目录添加到路径中,但似乎我需要做其他事情。有人对这个问题有任何想法吗?谢谢。

【问题讨论】:

  • 这个错误是在链接时还是编译时?这看起来像是两件事之一。您需要包含另一个文件,或者您没有链接到所需的库。我相信您遇到了链接错误,您应该检查这些编译指示。 #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib")
  • 你的构建命令是什么?
  • 这是一个链接错误。我已经检查了所有这些编译指示,但我得到了同样的错误。
  • 我在 Path 变量中添加了编译指示的 lib 文件夹目录,但编译器似乎无法识别库名称。
  • 我刚刚将此源粘贴到一个 VS2012 空项目中,它可以正确构建、链接和所有内容。你用的是什么工具链?

标签: c sockets winsock


【解决方案1】:

为了使编译成功,您需要执行以下更改:

  1. 在您的源文件中,您必须在包含标题之前定义#define _WIN32_WINNT 0x0501。详情请参考之前的post

  2. 来源中有错别字。请将第 9 行的 closessocket 替换为 closesocket。 121 的源文件。

  3. 使用以下命令编译

    gcc -o socketServer socketServer.c -lws2_32 -lwsock32 -L $MinGW\lib

其中$MinGW是MinGW软件的安装目录

通过这些更改,我能够成功编译您的代码。

【讨论】:

  • 非常感谢您提供的信息。它对我帮助很大,我的程序现在正在运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多