3

昨天我尝试用 C++ 制作一个套接字服务器,但编译时出现错误。错误:

错误 6 错误 LNK2019:未解析的外部符号 _ imp _socket@12 在函数“public: static unsigned long __cdecl Env::GetSocket(void)”(?GetSocket@Env@@SAKXZ) C:\Users\JoshuaTha\Documents\Visual 中引用工作室 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5

错误 5 错误 LNK2019:未解析的外部符号 _ imp _listen@8 在函数“public: void __thiscall Network::Start(void)”(?Start@Network@@QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010 中引用\Projects\HabboV5\HabboV5\Network.obj HabboV5

错误 4 错误 LNK2019:未解析的外部符号 _ imp _htons@4 在函数“public: void __thiscall Network::Start(void)”(?Start@Network@@QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010 中引用\Projects\HabboV5\HabboV5\Network.obj HabboV5

错误 3 错误 LNK2019:未解析的外部符号 _ imp _bind@12 在函数“public: void __thiscall Network::Start(void)”(?Start@Network@@QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010 中引用\Projects\HabboV5\HabboV5\Network.obj HabboV5

错误 2 错误 LNK2001: 无法解析的外部符号“public: static class Network * Env::Network” (?Network@Env@@2PAV0@A) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\ HabboV5.obj HabboV5

错误 7 error LNK1120: 5 unresolved externals C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\Debug\HabboV5.exe HabboV5

我的主要 .cpp 类:

// HabboV5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "Env.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout.write("hi", 2);
cout << "Hello World!" << endl;

Env::Network = new Network();
Env::Network->Start();

while (1)
{
    char input[256];
    cin.getline(input, 256);
}
}

网络.h:

#pragma once
#include <WinSock2.h>

class Network
{
private:
    SOCKET socket;
public:
    Network(void);
    void Start();
};

网络.cpp:

#include "StdAfx.h"
#include "Network.h"
#include <WinSock2.h>
#include "Env.h"

Network::Network(void)
{
}

void Network::Start()
{
    this->socket = Env::GetSocket();

    SOCKADDR_IN sInformation;

    sInformation.sin_family = AF_INET;
    sInformation.sin_addr.s_addr = INADDR_ANY;
    sInformation.sin_port = htons(30000);

    bind(this->socket, (SOCKADDR*) (&sInformation), sizeof(sInformation));
    listen(this->socket, 10);
}

环境.h:

#include "stdafx.h"
#include "Network.h"
#include <WinSock2.h>

class Env
{
public:
    static Network* Network;

    static DWORD GetSocket()
    {
        return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    }
};
4

1 回答 1

6

在链接器选项(在项目上右键单击、链接器、输入)中,您需要将wsock32.lib或添加ws2_32.lib到输入文件列表中。

于 2013-06-12T16:04:42.340 回答