0

I have two Project, one is called NetCommon while the other called NetServer. The configuration type of NetCommon is .lib file while NetServer Project is .exe file. I have also add NetServer dependencies via Build Dependencies -> Project Dependencies -> Depends on and choose NetCommon. I also added the directories which contains all the required libraries in Properties -> VC++ Directories -> Include Directories. Everything works fine when I build the NetCommon Project. However, it said error LNK2019: unresolved external symbol when I try to build the NetServer Project. I quite sure that I have define all the member functions. I have no idea why it still state such errors.

Folder hierarchy

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

Error Messages

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::basic_stream_socket<class asio::ip::tcp,class asio::any_io_executor>,class asio::io_context &>(class NetConnection &,enum NetConnection::Owner &&,class asio::basic_stream_socket<class asio::ip::tcp,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::ip::tcp::socket a_socket, 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"),
            [this](asio::error_code ec, size_t len)
            {
                if (ec)
                    Disconnect();
            }
    );
    else
        Disconnect();
}

NetServer.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;
}
  • Where is your `NetConnection.cpp` file? The error says it cant find the implementation of your constructor for `NetConnection()` – drescherjm Jun 28 '21 at 12:36
  • ***I have also add NetServer dependencies via Build Dependencies -> Project Dependencies -> Depends on and choose NetCommon.*** I don't believe this is athe correct approach. You need to add the library for this project to the linker settings of the .exe project for each configuration. – drescherjm Jun 28 '21 at 12:39
  • Sorry, I forgot to upload it. I have uploaded it back and you are right. I also need to add the library to the linker via `Properties` -> `Linker` -> `Input` -> `Additional Dependencies` and add the `NetCommon.lib`. Thank you – TPY-Lawrence Jun 28 '21 at 15:43

0 Answers0