Skip to content
Advertisement

How to link to Winsock when cross-compiling a Windows app on Linux?

I am porting a Cmake project from Linux to Windows. It uses sockets, so uses pthread in Linux and needs Winsock in Windows.

The project is being compiled in Linux, using cross-compilation to create Windows binaries.

The project is already compiling fine, and there is a switch to link pthreads when building for Linux and winsock for Windows.

However, there is a linking issue with sockets, it seems that winsock is not found.

Here is part of the Cmake file I am using :

find_library(lib1 wsock32)
find_library(lib2 ws2_32)

message("${lib1}  ${lib2}")

if (WIN32)
    target_link_libraries (TaskTool wsock32 ws2_32)
endif(WIN32)

And here is the output I get :

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'

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.

  • Is there a standard package I can install to have it? (like apt-get install windowssdk…).

  • Should I copy it manually and link it from a Windows installation?

  • Is there something I need to add to the Cmake toolchain file?

Here is the toolchain file I am using :

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++)

Advertisement

Answer

After some while testing various configuration, I finally found out how to solve the issue :

  1. Get ws2_32.lib directly from Windows installation. No need to install Windows SDK.
  2. Link to it using a path name :

    target_link_libraries (TaskTool ToolsLib "${TOOLS_ROOT}/ToolsLib/lib/ws2_32.lib")

  3. Include directly on the dependencies the module that will need it. Without cross-compiling that was not necessary, but now it is.

With these 3 steps the program builds fine. However, it doesn’t run on Windows. In order to do so, additional changes are necessary :

  1. Link statically C runtime environment :

target_link_libraries (TaskTool "-static-libgcc" "-static-libstdc++")

  1. Provide libwinpthread-1.dll in the same folder as the executable. I didnt’ find it on my OS, so I downloaded it.
Advertisement