title: How to link to Winsock when cross-compiling a Windows app on Linux?
body: <p>I am porting a Cmake project from Linux to Windows. It uses sockets, so uses pthread in Linux and needs Winsock in Windows.</p>
  
  <p>The project is being compiled in Linux, using cross-compilation to create Windows binaries.</p>
  
  <p>The project is already compiling fine, and there is a switch to link pthreads when building for Linux and winsock for Windows.</p>
  
  <p>However, there is a linking issue with sockets, it seems that winsock is not found.</p>
  
  <p>Here is part of the Cmake file I am using :</p>
  
  <pre><code>find_library(lib1 wsock32)
  find_library(lib2 ws2_32)
  
  message("${lib1}  ${lib2}")
  
  if (WIN32)
      target_link_libraries (TaskTool wsock32 ws2_32)
  endif(WIN32)
  </code></pre>
  
  <p>And here is the output I get :</p>
  
  <pre><code>lib1-NOTFOUND  lib2-NOTFOUND
  -- Configuring done
  -- Generating done
  -- Build files have been written to: 
  Linking CXX executable TaskTool.exe
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x522): referência indefinida a `_imp__socket@12'
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x559): referência indefinida a `_imp__setsockopt@20'
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x596): referência indefinida a `_imp__bind@12'
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x5b0): referência indefinida a `_imp__inet_addr@4'
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x5c3): referência indefinida a `_imp__htons@4'
  ToolsLib/libToolsLib.a(networkwaker.cpp.obj):networkwaker.cpp:(.text+0x614): referência indefinida a `_imp__sendto@24'
  </code></pre>
  
  <p>How is the proper way to add/link to winsock? I understand that winsock is not readily available in Linux as it is a Windows library.</p>
  
  <ul>
  <li><p>Is there a standard package I can install to have it? (like apt-get install windowssdk...).</p></li>
  <li><p>Should I copy it manually and link it from a Windows installation?</p></li>
  <li><p>Is there something I need to add to the Cmake toolchain file?</p></li>
  </ul>
  
  <p>Here is the toolchain file I am using :</p>
  
  <pre><code>SET(CMAKE_SYSTEM_NAME Windows)
  SET(CMAKE_C_COMPILER   /usr/bin/i686-w64-mingw32-gcc)
  SET(CMAKE_CXX_COMPILER /usr/bin/i686-w64-mingw32-g++)
  </code></pre>