title: Windows Socket Encapsulation - Unresolved external symbol
body: <p>I'm learning winsockets and got a linker error when calling the <code>socket(af, type, protocol)</code> function in a constructor.</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include "Server.h"
  
  int main(int argc, char **argv) {
      nmd::Server *server = new nmd::Server();
  
      //TODO: Logic
  
      delete server;
      server = nullptr;
      return 0;
  }
  </code></pre>
  
  <p>Server.h</p>
  
  <pre><code>#pragma once
  
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  
  namespace nmd {
  class Server {
  public:
      Server();
      virtual ~Server();
  private:
      SOCKET socket_;
  };
  }
  </code></pre>
  
  <p>Server.cpp</p>
  
  <pre><code>#include "Server.h"
  
  nmd::Server::Server() {
      socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (INVALID_SOCKET == socket_)
          throw 1; // TODO: proper exception handling
  }
  
  nmd::Server::~Server() {}
  </code></pre>
  
  <p>When I try to build it, an unresolved external symbol error occur. In detail:</p>
  
  <pre><code>LNK2019 unresolved external symbol __imp__socket@12 referenced in function "public: __thiscall nmd::Server::Server(void)" (??0Server@nmd@@QAE@XZ)
  </code></pre>
  
  <p>What am I missing here? I want to know the source of this problem, not only the solution. What makes the compiler to belive that <code>nmd::Server::Server()</code> is undefined when I put the <code>socket(...)</code> function in it?</p>