Cross Compiling for Windows

Step 1: Install minGW-w64 compile tools

The MinGW project provides a GNU compiler toolchain for Microsoft Windows. The MinGW-w64 project is a newer fork of the MinGW project that provides a more complete Win32 API implementation, a C++11 threads library, and Win64. 32-bit programs can also be built with MinGW-w64.

On Ubuntu install the mingw-w64 package. This is a meta package that actually installs binutils, g++, and gcc for both 32 and 64 bit targets.

sudo apt-get install mingw-w64

NOTE: You will likely need to define the NTDDI_VERSIONand _WIN32_WINNT macros. See this page about Defining Windows Version headers. For my purpose, I use -D_WIN32_WINNT=0x0600

Step 2: Create a Meson cross build file

mingw_cross.txt

name = 'mingw'  
c = '/usr/bin/i686-w64-mingw32-gcc'  
cpp = '/usr/bin/i686-w64-mingw32-g++'  
ar = '/usr/bin/i686-w64-mingw32-ar'  
strip = '/usr/bin/i686-w64-mingw32-strip'  
exe_wrapper = 'wine' # A command used to run generated executables.  

Step 3: Invoke Meson

mkdir win32-build  
meson win32-build --cross-file mingw_cross.txt  

Step 4: Compile

cd win32-build  
ninja  

Another tip I found was to statically link my executables. I did this by adding link_args : '-static' in my meson.build file.

Paul Soucy

Read more posts by this author.