title: Visual Studio 2019 "error LNK2019: unresolved external symbol"
body: <p>I have two Project, one is called <code>NetCommon</code> while the other called <code>NetServer</code>. The configuration type of <code>NetCommon</code> is <code>.lib</code> file while <code>NetServer</code> Project is <code>.exe</code> file. I have also add <code>NetServer</code> dependencies via <code>Build Dependencies</code> -&gt; <code>Project Dependencies</code> -&gt; <code>Depends on</code> and choose <code>NetCommon</code>. I also added the directories which contains all the required libraries in <code>Properties</code> -&gt; <code>VC++ Directories</code> -&gt; <code>Include Directories</code>. Everything works fine when I build the <code>NetCommon</code> Project. However, it said <code>error LNK2019: unresolved external symbol</code> when I try to build the <code>NetServer</code> Project. I quite sure that I have define all the member functions. I have no idea why it still state such errors.</p>
  <p>Folder hierarchy</p>
  <pre><code>Networking
  |---NetCommon
      |---NetCommon.h
      |---NetConnection.h
      |---NetConnection.cpp
  |---NetServer
      |---NetServer.h
      |---NetServer.cpp
      |---SimpleServer.cpp
  </code></pre>
  <p>Error Messages</p>
  <pre><code>Build started...
  1&gt;------ Build started: Project: NetServer, Configuration: Debug Win32 ------
  1&gt;NetServer.cpp
  1&gt;SimpleServer.cpp
  1&gt;Generating Code...
  1&gt;NetServer.obj : error LNK2019: unresolved external symbol &quot;public: __thiscall NetConnection::NetConnection(enum NetConnection::Owner,class asio::basic_stream_socket&lt;class asio::ip::tcp,class asio::any_io_executor&gt;,class asio::io_context &amp;)&quot; (??0NetConnection@@QAE@W4Owner@0@V?$basic_stream_socket@Vtcp@ip@asio@@Vany_io_executor@3@@asio@@AAVio_context@3@@Z) referenced in function &quot;void __cdecl std::_Construct_in_place&lt;class NetConnection,enum NetConnection::Owner,class asio::basic_stream_socket&lt;class asio::ip::tcp,class asio::any_io_executor&gt;,class asio::io_context &amp;&gt;(class NetConnection &amp;,enum NetConnection::Owner &amp;&amp;,class asio::basic_stream_socket&lt;class asio::ip::tcp,class asio::any_io_executor&gt; &amp;&amp;,class asio::io_context &amp;)&quot; (??$_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&gt;NetServer.obj : error LNK2019: unresolved external symbol &quot;public: __thiscall NetConnection::~NetConnection(void)&quot; (??1NetConnection@@QAE@XZ) referenced in function &quot;public: void * __thiscall NetConnection::`scalar deleting destructor'(unsigned int)&quot; (??_GNetConnection@@QAEPAXI@Z)
  1&gt;C:\Users\user\source\repos\Networking\Debug\NetServer.exe : fatal error LNK1120: 2 unresolved externals
  1&gt;Done building project &quot;NetServer.vcxproj&quot; -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
  </code></pre>
  <p>NetCommon.h</p>
  <pre><code>#pragma once
  
  #ifdef _WIN32
  #define _WIN32_WINNT 0x0A00
  #endif
  
  #define ASIO_STANDALONE
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;deque&gt;
  #include &lt;mutex&gt;
  #include &lt;thread&gt;
  #include &lt;asio.hpp&gt;
  #include &lt;asio/ts/buffer.hpp&gt;
  #include &lt;asio/ts/internet.hpp&gt;
  </code></pre>
  <p>NetConnection.h</p>
  <pre><code>#pragma once
  
  #include &quot;NetCommon.h&quot;
  
  class NetConnection
  {
  public:
      enum class Owner
      {
          Server,
          Client
      };
  public:
      NetConnection(Owner a_owner, asio::ip::tcp::socket a_socket, asio::io_context&amp; 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&amp; m_context;
      // buffer for read and write
      std::array&lt;char, 256&gt; m_messageIn;
      std::array&lt;char, 256&gt; m_messageOut;
  };
  </code></pre>
  <p>NetConnection.cpp</p>
  <pre><code>#include &quot;NetConnection.h&quot;
  
  NetConnection::NetConnection(NetConnection::Owner a_owner, asio::ip::tcp::socket a_socket, asio::io_context&amp; 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 &lt;&lt; &quot;Connection is closed.&quot; &lt;&lt; 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(&quot;Hello World!\n\r&quot;),
              [this](asio::error_code ec, size_t len)
              {
                  if (ec)
                      Disconnect();
              }
      );
      else
          Disconnect();
  }
  
  </code></pre>
  <p>NetServer.h</p>
  <pre><code>#include &quot;NetServer.h&quot;
  
  // 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 &lt;&lt; &quot;[SERVER] Started!\n&quot;;
  
      // 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 &lt;&lt; &quot;[SERVER] New Connection: &quot; &lt;&lt; socket.remote_endpoint() &lt;&lt; &quot;\n&quot;;
                  // keep the new connection alive
                  std::shared_ptr&lt;asio::ip::tcp::socket&gt; newConnection = std::make_shared&lt;asio::ip::tcp::socket&gt;(std::move(socket));
                  m_connections.push_back(newConnection);
  
                  std::shared_ptr&lt;NetConnection&gt; newNetConnecion = std::make_shared&lt;NetConnection&gt;(NetConnection::Owner::Server, std::move(socket), m_context);
                  //m_newConnections.push_back(newNetConnecion);
              }
              else
                  std::cout &lt;&lt; &quot;[SERVER] New Connection Error: &quot; &lt;&lt; ec.message() &lt;&lt; &quot;\n&quot;;
  
              // wait for another connection
              WaitForConnection();
          }
      );
  }
  </code></pre>
  <p>SimpleServer.cpp</p>
  <pre><code>#include &quot;NetServer.h&quot;
  
  int main()
  {
      // construct a server obj
      NetServer server;
      // start the server
      server.Start();
      // start listening
      while (true);
      return 0;
  }
  </code></pre>