[Armadillo.Tech ~]$

Cross-compiling for Windows on Linux

Linux-based operating systems are the best thing since sliced bread, but you do have to think of people using other OSes like Windows. With Java or Python, that’s pretty much a non-issue. If you write code in the C family languages or Fortran, you have to compile for the exact platform your software will run on. You could go through the trouble to spin up a Windows VM, but why leave the comfort of Linux behind? You can cross-compile instead!

To compile Windows executables on Linux, you need to install MinGW-w64.

On Debian, install the MinGW compilers with:

apt install mingw-w64 gobjc-mingw-w64 gobjc++-mingw-w64-i686 gobjc++-mingw-w64-x86-64 gfortran-mingw-w64

On Fedora:

dnf install mingw64-filesystem mingw32-gcc mingw32-gcc-c++ mingw32-gcc-objc mingw32-gcc-objc++ mingw32-gcc-gfortran mingw64-gcc mingw64-gcc-c++ mingw64-gcc-objc mingw64-gcc-objc++ mingw64-gcc-gfortran

Now that you have everything installed, here is how you can cross-compile under several different scenarios. I have included directions for single source files, Autoconf projects, and CMake projects.

Single source files

64-bit

Run one of these commands:

C: x86_64-w64-mingw32-gcc -o yourprogram.exe yourprogram.c

C++: x86_64-w64-mingw32-g++ -o yourprogram.exe yourprogram.cpp

Objective-C: x86_64-w64-mingw32-gcc -o yourprogram.exe yourprogram.m

Objective-C++: x86_64-w64-mingw32-g++ -o yourprogram.exe yourprogram.mm

Fortran: x86_64-w64-mingw32-gfortran -o yourprogram.exe yourprogram.f90

32-bit

Run one of these commands:

C: i686-w64-mingw32-gcc -o yourprogram.exe yourprogram.c

C++: i686-w64-mingw32-g++ -o yourprogram.exe yourprogram.cpp

Objective-C: i686-w64-mingw32-gcc -o yourprogram.exe yourprogram.m

Objective-C++: i686-w64-mingw32-g++ -o yourprogram.exe yourprogram.mm

Fortran: i686-w64-mingw32-gfortran -o yourprogram.exe yourprogram.f90

Autoconf projects:

Run one of these commands:

32-bit: ./configure --host=i686-w64-mingw32; make

64-bit: ./configure --host=x86_64-w64-mingw32; make

CMake projects:

If you really want to do it with CMake, read the official community wiki.