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 :

JavaScript

And here is the output I get :

JavaScript

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 :

JavaScript

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