title: ws2_32.lib vs. libws2_32.a, what's the difference and how to link libws2_32 to NB project?
body: <p>I use NetBeans, Windows and Cygwin with g++ compiler.</p>
  
  <p>I'm examining Windows Sockets 2. I do everything that is written in MS <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms737629%28v=vs.85%29.aspx" rel="nofollow noreferrer">manual</a>. I have a code (mostly from this manual):</p>
  
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  int main() {
  
    WSADATA wsaData;
  
    int iResult;
  
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
    if (iResult != 0) {
       printf("WSAStartup failed: %d\n", iResult);
       return 1;
    }
    else cout &lt;&lt; "Initialization OK.";
  
    return 0;
  }
  </code></pre>
  
  <p>And I have a problem when I try to run the project:</p>
  
  <pre><code>undefined reference to `_WSAStartup@8'
  </code></pre>
  
  <p>I understand that <code>Ws2_32.lib</code> is missing. This is because I do not have Windows SDK installed. But before installing it I want to try out tools that Cygwin offers. It has all the <code>w32api</code> header files, I have them in:</p>
  
  <pre><code>C:\cygwin\usr\include\w32api
  </code></pre>
  
  <p>And it has some <code>w32api</code> almost <code>.lib</code> files in the directory:</p>
  
  <pre><code>C:\cygwin\lib\w32api
  </code></pre>
  
  <p>But all these lib files are different, they have <code>.a</code> extension and a little bit different name, like:</p>
  
  <pre><code>libws2_32.a  // in Cygwin
     vs.
  ws2_32.lib   // in Windows    
  </code></pre>
  
  <p>When I use Cygwin terminal to create an <code>.exe</code> file, everything works fine. The commands I input are:</p>
  
  <pre><code>cd C:\\c++\\myProgram           // go to the dir
  g++ myProgram.cpp -lws2_32      // compile using -l option to link libws2_32.a
  </code></pre>
  
  <p>And after it I get <code>a.exe</code> file. I run it and it works:</p>
  
  <pre><code>./a.exe    // Initialization OK.
  </code></pre>
  
  <p>But as I said I use NetBeans. And if I try to run the project from NB (<code>[F6]</code> button) I always have this error <code>undefined reference to '_WSAStartup@8'</code>.</p>
  
  <p>I've tried already everything I could find on NB forums. I've tried to link <code>libws2_32.a</code> to my project this way. I go to:</p>
  
  <pre><code>File -&gt; Project Properties -&gt; Linker -&gt; Libraries
  </code></pre>
  
  <p>And there are three options:</p>
  
  <ul>
  <li>Add Library...</li>
  <li>Add Library File...</li>
  <li>Add Option...</li>
  </ul>
  
  <p>I've tried them all. I've tried to link both just Library and Library File. I've also tried 
  to add such an option in the <code>Add Option...</code> button:</p>
  
  <pre><code>Add Option... -&gt; Other option -&gt;    // and I input here "-lws2_32"
  </code></pre>
  
  <p>But whatever I do I can't run the project from NB, I get error <code>undefined reference to '_WSAStartup@8'</code>. </p>
  
  <p>So my questions are:</p>
  
  <p>1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.</p>
  
  <p>2) What is the difference between Windows <code>.lib</code> files and Cygwin <code>.a</code> files? Is it better to install Windows SDK and just forget about those <code>.a</code> files? Everything I could find so far about them on Cygwin site is this:</p>
  
  <blockquote>
    <p>The import library is a regular UNIX-like .a library, but it only
    contains the tiny bit of information needed to tell the OS how your
    program interacts with ("imports") the dll. This information is linked
    into your .exe. This is also generated by dlltool.</p>
  </blockquote>
  
  <p>3) Is it possible to use <code>#pragma comment(lib, "libws2_32.a")</code> to link <code>.a</code> files? I've tried but didn't get success results.</p>
  
  <p><strong>UPD:</strong></p>
  
  <p>The answer for the 3rd question -> <a href="https://stackoverflow.com/questions/1685206/pragma-commentlib-xxx-lib-equivalent-under-linux">#pragma comment(lib, &quot;xxx.lib&quot;) equivalent under Linux?</a></p>