Słomkowski's technical musings

Playing with software, hardware and touching the sky with a paraglider.

Compiling C++ application for Windows target under Linux


Since forever, I have used Arch Linux for day-to-day computing. Nonetheless, from time to time I need to write some simple code in C++ for Windows. Switching back and forth between operating systems is cumbersome, so compiling under a virtual machine is as well.

The Windows port of GCC is named MinGW (Minimalistic GNU for Windows). Quite surprisingly, it is available in the repositories of major Linux distributions, with a wealth of open-source libraries cross-compiled for the Windows platform.

Leveraging this tool, writing multi-platform software can be almost frictionless. It works well in the IDE CLion too. For demonstration purposes, I provide a sample repository which contains a CMake project of a DLL and Windows executables built with MinGW.

Installing MinGW toolchain

Using Arch Linux, I recommend using the ownstuff unofficial repository. The compiler is packaged in a group named mingw-w64. You might try compiling it from the AUR, but it can be a real pain. There are also many packages available for various dependency libraries; all package names start with mingw-w64-. For more details, please refer to the Arch Wiki.

MinGW is also available in the official repository under Debian - the meta-package gcc-mingw-w64. There are numerous precompiled libraries, all having mingw in their package names. There is also an entry on the Debian Wiki about this topic.

General configuration

The most important thing is to define the compiler executables and set CMAKE_SYSTEM_NAME:

cmake_minimum_required(VERSION 3.0)

project(mingw-test)

set(CMAKE_SYSTEM_NAME Windows)

SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
set(CMAKE_RANLIB i686-w64-mingw32-ranlib)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)

Then, you simply define the executable. This requires no further comments.

add_executable(example example.cpp)

To cross-compile a dynamic loadable library, you use add_library. However, for convenience, it is better to set PREFIX and SUFFIX to empty strings. This way, you can use the whole filename (with the .dll extension) as the target name.

add_library(shared_lib.dll SHARED shared_lib.cpp shared_lib.h)
set_target_properties(shared_lib.dll PROPERTIES
        PREFIX ""
        SUFFIX ""
        LINK_FLAGS "-Wl,--add-stdcall-alias"
        POSITION_INDEPENDENT_CODE 0 # this is to avoid MinGW warning; 
        # MinGW generates position-independent-code for DLL by default
)

To link the executable with the aforementioned DLL, you add:

target_link_libraries(example shared_lib.dll)

Using pkg-config

The common way of defining library compilation flags and directories under Unix is using the pkg-config tool. This way, your codebase is not dependent on the exact layout of directories of the distribution. Each library provides a .pc file with build parameters such as header locations, compiler flags, etc. Many packages compiled for MinGW also provide .pc files, at least under Debian and Arch Linux.

For example, let’s add zlib as a dependency:

Load PkgConfig package and find zlib:

include(FindPkgConfig)
find_package(PkgConfig REQUIRED)

pkg_check_modules(ZLIB "zlib")

Add dependencies to example executable:

include_directories(${ZLIB_INCLUDE_DIRS})
target_link_libraries(example ${ZLIB_LIBRARIES})

Adding runtime libraries to Wine

When you try to run your freshly cross-compiled program under Wine, it will probably fail with a message like this:

0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"your-program.exe") not found

It means that Wine cannot find the runtime libraries on which your program depends. They are usually located under /usr/i686-w64-mingw32/bin. You’ll need to add this path to the PATH variable within the Wine subsystem.

To do so, edit the file ~/.wine/system.reg. This file represents the Windows Registry under Wine. Find the PATH variable definition under [System\\CurrentControlSet\\Control\\Session Manager\\Environment] section. Append the library path translated into a Windows-like path: Z:\usr\i686-w64-mingw32\bin so it looks like this:

"PATH"=str(2):"C:\\windows\\system32;C:\\windows;C:\\windows\\system32\\wbem;Z:\\usr\\i686-w64-mingw32\\bin"

Running with Wine under Clion

Your CMake project should be loaded by Clion without issues. Windows-specific headers like windows.h are also available. The build will configure itself automatically, but running the target executables will not. Fortunately, you can modify the generated Run/Debug configurations to call Wine, as shown in the picture:

Run/Debug configuration window.
You can easily configure the target to run under Wine within Clion.