Gentoo Wiki


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Please format this article according to the guidelines and Wikification suggestions, then remove this notice {{Wikify}} from the article


Cross-compiling with MinGW


Cross compiling is a technique to create a binary for another run-time environment. For example "cross compile from linux to windows", means using a linux system to build files which can run on windows. This HOWTO will outline the major points on cross compiling using "emerge".

Cross compiling

This HOWTO only refers to "the gentoo way" of cross compilation.

A simple "from source code" compilation of compilers, can be found here:

Free Compilers and Cross-Compilers for Linux and Windows (GCC 4.2.3)
Free Compilers and Cross-Compilers for Linux and Windows (GCC 4.3.0 Initial Results)

using emerge

Gentoo has a tool named "crossdev", so go ahead an install it with:

emerge -va sys-devel/crossdev

This installs only the environment installer for the cross compilation toolchain, not the toolchain itself. We want to create Windows (32 bit) binaries, so we need the mingw32 toolchain. But before we can add it to our system, we need to provide a place for an overlay. This overlay will contain the ebuild created by crossdev. If you have not used an overlay before, add it to your /etc/make.conf:

echo 'PORTDIR_OVERLAY="/usr/local/overlays/crossdev"' >> /etc/make.conf

If you have already used an overlay from layman, make sure the PORTDIR_OVERLAY entry is above the "source /usr/local/overlays/make.conf" entry of layman.

The above is not correct. It appears that layman will change the order of the directories which causes problems for crossdev. I have found that the way to prevent this is to change this from something like:

 PORTDIR_OVERLAY="/usr/i686-mingw32/usr/portage /usr/local/portage"
 source /usr/portage/local/layman/make.conf

to something like:

 source /usr/portage/local/layman/make.conf
 PORTDIR_OVERLAY="/usr/i686-mingw32/usr/portage /usr/local/portage $PORTDIR_OVERLAY"

This will ensure that the layman overlay directories will always be after the overlay directories specified in /etc/mmake.conf which will prevent it from causing problems for crossdev.

If you already had an overlay specified in PORTDIR_OVERLAY, you can either use this or add the location for the crossdev overlay at the first position in PORTDIR_OVERLAY.

Once you have added the overlay and created the needed directory, install the toolchain:

crossdev i686-mingw32

This installs the latest mingw32 packages for you and adds the ebuilds to your crossdev overlay. Note: This takes quite a lot of time depending on your computers power.

The last step is to emerge the packages you want to have as cross compiled programs or libraries. There is a bash script that is documented in the Gentoo Cross Development Guide that should be used for this named xmerge. This script should be executable and in your path. To use this script you need to set the SYSROOT environment variable:

 export SYSROOT="/usr/i686-mingw32"

This can be added to the root users .bashrc so that it is set automatically or you can set it before you run xmerge. Then to emerge something with the mingw tool chain you just say:

 xmerge whatever

The Gentoo Cross Development Guide has more information about what environment variables affect this. But it does not have anything about how overlays should be setup.

For most things the above will work fine. However there are some things for which this will not work. One such case is Qt3. The ebuilds for this are not setup for cross compiling and will pull in a bunch of X11 and *nix dependencies such as CUPS which are clearly not correct for a Windows build. For cases like this it may be necessary to create custom ebuilds to build the windows specific version of the library or program.

Anyone who figures out a good way to handle cases like Qt please post your solutions here.

HOWTO MinGW and Qt4 I have had success building a Windows binary on Gentoo.

using paludis

# usage of contrarius is not recommended at this time
# use your local repository to setup a cross* category
mkdir -p cross-i686-pc-mingw32
echo cross-i686-pc-mingw32 >> profiles/categories

# link to the following ebuilds in portage for this category
for i in w32api mingw-runtime
  do ln -s ${GENTOOREP}/dev-util/$i cross-i686-pc-mingw32/$i
for i in gcc gdb binutils
  do ln -s ${GENTOOREP}/sys-devel/$i cross-i686-pc-mingw32/$i

# set options for minimal toolchain
echo "CROSSCOMPILE_OPTS: cross-i686-pc-mingw32/* headers-only" >> /etc/paludis/use.conf
echo "cross-i686-pc-mingw32/gcc -* nocxx" >> /etc/paludis/use.conf

# headers
paludis -i cross-i686-pc-mingw32/{w32api,mingw-runtime}

# minimal gcc
paludis -i cross-i686-pc-mingw32/{binutils,gcc}

# deactivate use flags as needed
sed -i -e 's/^cross-i686-pc-mingw32\/gcc.*/cross-i686-pc-mingw32\/gcc -nls -gcj -gtk/' \
sed -i -e 's/^CROSSCOMPILE_OPTS/#\0/' /etc/paludis/use.conf

# full toolchain
paludis -i cross-i686-pc-mingw32/{w32api,mingw-runtime,gcc,gdb}

--Cmuelle8 01:47, 10 July 2008 (UTC)


Bug: errors when trying to run crossdev i686-mingw32
Probably because of a bug in the toolchain it gives some errors

If you get errors when trying to run crossdev i686-mingw32 try this.

CFLAGS=-O2 CXXFLAGS=-O2 FEATURES=nostrip crossdev -t mingw32

And probably -march likely to break such things. Probably it is a bug in the toolchain; stripping flags just ignores the problem.

FIXME: Please verify this.

If you have errors while emerging binutils with crossdev, try to disable some USE flags :

USE="-multislot -multitarget -nls" crossdev -t i686-mingw32


To test mingw installation we can use this Win32 source file:

File: wintest.c
  #include <windows.h>
  int WINAPI WinMain (HINSTANCE hInstance, 
                       HINSTANCE hPrevInstance, 
                       PSTR szCmdLine, 
                       int iCmdShow) 
     MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
     return (0);

To compile it we should run:

Code: Compile and run your program
i686-mingw32-gcc wintest.c -o wintest.exe -mwindows
wine wintest.exe

The -mwindows switch is needed to create Windows executables instead of console applications.

See also

External links

Retrieved from ""

Last modified: Thu, 10 Jul 2008 14:39:00 +0000 Hits: 9,731