微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Visual Studio 2019“错误 LNK2019:无法解析的外部符号”

如何解决Visual Studio 2019“错误 LNK2019:无法解析的外部符号”

我有两个项目,一个NetCommon,另一个NetServerNetCommon 的配置类型是 .lib 文件,而 NetServer 项目是 .exe 文件。我还通过 NetServer -> Build Dependencies -> Project Dependencies 添加Depends on 依赖项并选择 NetCommon。我还在 Properties -> VC++ Directories -> Include Directories添加了包含所有必需库的目录。当我构建 NetCommon 项目时,一切正常。但是,当我尝试构建 error LNK2019: unresolved external symbol 项目时,它显示 NetServer。我很确定我已经定义了所有的成员函数。我不知道为什么它仍然指出这样的错误

文件夹层次结构

Networking
|---NetCommon
    |---NetCommon.h
    |---NetConnection.h
    |---NetConnection.cpp
|---NetServer
    |---NetServer.h
    |---NetServer.cpp
    |---SimpleServer.cpp

错误信息

Build started...
1>------ Build started: Project: NetServer,Configuration: Debug Win32 ------
1>NetServer.cpp
1>SimpleServer.cpp
1>Generating Code...
1>NetServer.obj : error LNK2019: unresolved external symbol "public: __thiscall NetConnection::NetConnection(enum NetConnection::Owner,class asio::basic_stream_socket<class asio::ip::tcp,class asio::any_io_executor>,class asio::io_context &)" (??0NetConnection@@QAE@W4Owner@0@V?$basic_stream_socket@Vtcp@ip@asio@@Vany_io_executor@3@@asio@@AAVio_context@3@@Z) referenced in function "void __cdecl std::_Construct_in_place<class NetConnection,enum NetConnection::Owner,class asio::io_context &>(class NetConnection &,enum NetConnection::Owner &&,class asio::any_io_executor> &&,class asio::io_context &)" (??$_Construct_in_place@VNetConnection@@W4Owner@1@V?$basic_stream_socket@Vtcp@ip@asio@@Vany_io_executor@3@@asio@@AAVio_context@4@@std@@YAXAAVNetConnection@@$$QAW4Owner@1@$$QAV?$basic_stream_socket@Vtcp@ip@asio@@Vany_io_executor@3@@asio@@AAVio_context@4@@Z)
1>NetServer.obj : error LNK2019: unresolved external symbol "public: __thiscall NetConnection::~NetConnection(void)" (??1NetConnection@@QAE@XZ) referenced in function "public: void * __thiscall NetConnection::`scalar deleting destructor'(unsigned int)" (??_GNetConnection@@QAEpaxI@Z)
1>C:\Users\user\source\repos\Networking\Debug\NetServer.exe : Fatal error LNK1120: 2 unresolved externals
1>Done building project "NetServer.vcxproj" -- Failed.
========== Build: 0 succeeded,1 Failed,1 up-to-date,0 skipped ==========

NetCommon.h

#pragma once

#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif

#define ASIO_STANDALONE

#include <iostream>
#include <vector>
#include <deque>
#include <mutex>
#include <thread>
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>

NetConnection.h

#pragma once

#include "NetCommon.h"

class NetConnection
{
public:
    enum class Owner
    {
        Server,Client
    };
public:
    NetConnection(Owner a_owner,asio::ip::tcp::socket a_socket,asio::io_context& a_context);
    ~NetConnection();
    void ReadMessage();
    void WriteMessage();
private:
    // id used for identification
    uint16_t m_id = 0;
    // owner of the connetion
    Owner m_owner;
    // socket for connection
    asio::ip::tcp::socket m_socket;
    // I/O context for asio
    asio::io_context& m_context;
    // buffer for read and write
    std::array<char,256> m_messageIn;
    std::array<char,256> m_messageOut;
};

NetConnection.cpp

#include "NetConnection.h"

NetConnection::NetConnection(NetConnection::Owner a_owner,asio::io_context& a_context,uint16_t id)
    : m_id(id),m_socket(std::move(a_socket)),m_context(a_context),m_messageIn({}),m_messageOut({})
{
    m_owner = a_owner;
}

NetConnection::~NetConnection()
{
    if (m_socket.is_open())
    {
        disconnect();
    }
    std::cout << "Connection is closed." << std::endl;
}

void NetConnection::ReadMessage()
{
    if (IsAlive())
        m_socket.async_read_some(asio::buffer(m_messageIn,256),[this](asio::error_code ec,size_t len)
            {
                if (!ec)
                {
                    std::cout.write(m_messageIn.data(),len);
                    ReadMessage();
                }
                else
                {
                    disconnect();
                }
            });
    else
        disconnect();
}

void NetConnection::WriteMessage()
{
    if (IsAlive())
        m_socket.async_write_some(asio::buffer("Hello World!\n\r"),size_t len)
            {
                if (ec)
                    disconnect();
            }
    );
    else
        disconnect();
}

网络服务器.h

#include "NetServer.h"

// initialize an acceptor and open a tcp socket at port
NetServer::NetServer() : m_acceptor(m_context,asio::ip::tcp::endpoint(asio::ip::tcp::v4(),60000)),m_socket(m_context),m_messageIn({})
{
}

NetServer::~NetServer()
{
    // stop the context
    m_context.stop();
}

// start the server
void NetServer::Start()
{
    // start thread for io
    m_thread = std::thread([this]() { m_context.run(); });

    std::cout << "[SERVER] Started!\n";

    // start waiting connection
    WaitForConnection();
}

void NetServer::WaitForConnection()
{
    // creates a socket and initiates an asynchronous accept operation to wait for a new connection.
    m_acceptor.async_accept(
        [this](std::error_code ec,asio::ip::tcp::socket socket)
        {
            if (!ec)
            {
                std::cout << "[SERVER] New Connection: " << socket.remote_endpoint() << "\n";
                // keep the new connection alive
                std::shared_ptr<asio::ip::tcp::socket> newConnection = std::make_shared<asio::ip::tcp::socket>(std::move(socket));
                m_connections.push_back(newConnection);

                std::shared_ptr<NetConnection> newNetConnecion = std::make_shared<NetConnection>(NetConnection::Owner::Server,std::move(socket),m_context);
                //m_newConnections.push_back(newNetConnecion);
            }
            else
                std::cout << "[SERVER] New Connection Error: " << ec.message() << "\n";

            // wait for another connection
            WaitForConnection();
        }
    );
}

SimpleServer.cpp

#include "NetServer.h"

int main()
{
    // construct a server obj
    NetServer server;
    // start the server
    server.Start();
    // start listening
    while (true);
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?