【问题标题】:C++ WinSock2 ErrorsC++ WinSock2 错误
【发布时间】:2013-06-08 19:39:58
【问题描述】:

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

错误 6 错误 LNK2019:未解析的外部符号 _imp_socket@12 在函数“public: static unsigned long __cdecl Env::GetSocket(void)”中引用 (?GetSocket@Env@@SAKXZ) C:\Users\JoshuaTha\Documents\Visual Studio 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

Error 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();
};

Network.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);
    }
};

【问题讨论】:

  • 您没有为定义套接字内容的 DLL 包含一个 implib。看看 msdn 什么 libs ho with winsock2

标签: c++ winsock2


【解决方案1】:

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

【讨论】:

  • 我真的不知道有什么区别,但是 wsock32.lib 对我不起作用,因为 getaddrinfo 和 freeaddrinfo 仍然存在链接器问题。使用 ws2_32.lib 确实可以立即使用
猜你喜欢
  • 2016-01-23
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多