4

我正在尝试制作一个简单的 UDP 套接字类来在 c++ 程序和 java 程序之间进行一些对话,所以我正在尝试制作一个处理所有 UDP 传输的套接字类,但我无法编译该程序,因为我得到大约 8 个 lnk2019 错误,我什至不知道它们是什么意思。我主要在 Java 中工作,只有在不得不大声笑的时候才使用 c++。我有 3 个文件的套接字头和代码。此外,Udp 套接字代码来自 rFactor-Nesim,因此套接字代码不是由我编写的。

UDPSocket.cpp

#include "UdpSocket.hpp"

#include <stdio.h>

UdpSocket::UdpSocket(const char* host, int port) 
: mHost(host), mPort(port)
{
}

UdpSocket::~UdpSocket(void)
{
}

void UdpSocket::Open()
{
if(WSAStartup(MAKEWORD(2, 0), &mWinsockData) != 0)
    fprintf(stderr, "WSAStartup() failed");   

if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    perror("socket() failed");   

memset(&mSocketAddress, 0, sizeof(mSocketAddress));
mSocketAddress.sin_family = AF_INET;
mSocketAddress.sin_addr.s_addr = inet_addr(mHost);
mSocketAddress.sin_port = htons(mPort);
}

void UdpSocket::Close()
{
closesocket(mSocket);  
WSACleanup();
}

void UdpSocket::Send(char* str, size_t length)
{
size_t result = sendto(mSocket, str, length, 0, 
    (struct sockaddr *) &mSocketAddress, sizeof(mSocketAddress));

if(result != length)
    perror("sendto() sent incorrect number of bytes");   
}

UDPSocket.hpp

#ifndef UDPSOCKET_HPP
#define UDPSOCKET_HPP

#include <WinSock.h>

class UdpSocket
{
public:
UdpSocket(const char* host, int port);
~UdpSocket(void);

void Send(char* str, size_t length);

void Open();
void Close();
private:
const char* mHost;
int mPort;
int mSocket;
struct sockaddr_in mSocketAddress;
WSADATA mWinsockData;
};

#endif // UDPSOCKET_HPP

和主要

#include "Socket/UdpSocket.hpp"
#include <iostream>


int Main(){
UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469);
testSocket->Open();
system("pause");
return 0;
}

任何帮助都会很棒。我对 C++ 不是很擅长,但我已经做了一点

控制台输出:

Error   1   error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   2   error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   3   error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ) UdpSocket.obj   SocketTest
Error   4   error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)  UdpSocket.obj   SocketTest
Error   5   error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close@UdpSocket@@QAEXXZ)    UdpSocket.obj   SocketTest
Error   6   error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   7   error LNK2019: unresolved external symbol __imp__sendto@24 referenced in function "public: void __thiscall UdpSocket::Send(char *,unsigned int)" (?Send@UdpSocket@@QAEXPADI@Z)  UdpSocket.obj   SocketTest
Error   8   error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup   MSVCRTD.lib SocketTest
Error   9   fatal error LNK1120: 8 unresolved externals C:\Users\Brendan\Documents\Visual Studio 2008\Projects\SocketTest\Debug\SocketTest.exe  SocketTest
4

2 回答 2

7

听起来你没有链接到 Winsock -Ws2_32.lib

如果您从命令行构建,请添加Ws2_32.liblink命令行。

如果从 Visual Studio 构建,请在项目配置对话框中查找链接器标志/设置。

于 2013-02-25T09:15:18.727 回答
0

当您在多个实现文件中有代码时,您需要编译所有这些实现文件并将生成的目标代码文件传递给链接器,链接器将它们(和其他东西)组合成一个可执行文件

仅包含模块的标头是不够的

c++(还)没有任何技术模块概念,因此包含头文件不会神奇地编译实现文件或将目标代码文件传递给链接器

这不是 c++ 标准的一部分,但它是日常工具使用的一部分

链接器告诉您您未能为它提供目标代码你的类成员函数温索克图书馆

该目标代码由库文件提供,在 Visual C++ 中通常具有文件扩展名“.lib”


一般来说,当你遇到一个神秘的错误时,只需在文档中查找错误号即可

在 Visual Studio 中,就像按 F1 键一样简单

于 2013-02-25T09:14:14.220 回答