title: Undefined reference to getaddrinfo
body: <p>I have been getting this error for quite some time now and Google has not been of much help either.</p>
  
  <p>I am a newbie to Winsock programming and trying to learn from online resources. I am trying to build a simple server using details on MSDN website. Whenever I compile the code (MinGW), I get the error mentioned in the title (<code>Undefined reference to getaddrinfo</code>). Below is the code:</p>
  
  <pre><code>#ifndef WIN32_LEAN_AND_MEAN
  #define WIN32_LEAN_AND_MEAN
  #endif
  #define WINVER WindowsXP
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;winsock.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  
  int main() {
      WSADATA wsaData;
      int iResult;
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if (iResult != 0) {
          printf("WSAStartup failed: %d\n", iResult);
          return 1;
      }
  
      #define DEFAULT_PORT "27015"
  
      struct addrinfo *result = NULL, *ptr = NULL, hints;
  
      ZeroMemory(&amp;hints, sizeof (hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the local address and port to be used by the server
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0) {
          printf("getaddrinfo failed: %d\n", iResult);
          WSACleanup();
          return 1;
      }
      return 0;
  }  
  </code></pre>
  
  <p>I am compiling with the following command:</p>
  
  <pre><code>gcc msdn_np.c -o msdn_np.exe -lWS2_32
  </code></pre>