Revision of HOWTO Build MinGW as a Cross Compiler for Win32 Development on GNU Linux from 2008, August 8 - 21:43

Printer-friendly version

TODO: Convert to the MediaWiki formatting as discussed at Converting Old MinGWiki Pages

Minimalist GNU for Win32 (updated for MinGW gcc 3.4.5)



This will NOT generate a compiler that is compatible with the standard windows version, nor the older Debian 3.4.2 version. This version attempts to use DW2 exceptions, which are not compatible with the more commonly used sjlj exceptions.

DW2 EH is superior to sjlj in terms of performance, but (apparently) still has problems on Win32, in particular throwing exceptions across DLL boundaries... maybe only when those DLLs do not know DW2 EH.

See here for a discussion on this:
and here to see when I realised this problem:

In the meantime, either add --enable-sjlj to configure or use Igor's script (link is at the bottom of this page). Hopefully DW2 will be working properly in the near future.

-- PaulHarris



The default version of the script uses DW2 EH. To switch back to sjlj EH search for the line


early in the script. Setting this variable to 1 will make the xcompiler use use sjlj EH.

Note also that all of the above is irrelevant to you when you are *not* using Exceptions, at least across DLL boundaries, in the first place. If that is your
situation (as is with the author of these lines; building Qt under native Win32 with stock gcc using sjlj and then using that Qt to create applications
under Linux via crosscompile for windows with a mingw gcc using DW2), there is no reason to not use DW2 EH.


How to build a Win32 x-compiler for Linux

To compile and link your Win32 executables under Linux you need a x-compiler. Luckily MinGW already provides all that is required. To actually get hold of such a x-compiler you may either try to get some binaries (someone please provide one or more links) or create one from the sources.

The rest of this page briefly describes how to create a x-compiler from the sources.

  1. Download at least the following (or newer) packages from the [MinGW download page |]:
   * [binutils |]
   * [gcc-core |]
   * [gcc-g++ |]
   * [mingw-runtime |]
   * [w32api |]

   You may want to add any of the following additional languages
   * [gcc-objc |]
   * [gcc-g77 |]
   * [gcc-java |]
   * [gcc-ada |]
   but you'd have to modify the provided shell-Script yourself.

  1. Get [this shell-Script], adjust at least BUILD_ROOT and PREFIX at the top of the script to match your setup. If you wish to build other compilers than gcc and g++ like e.g. g77 or gnat you'll search for the lang_* settings a bit further down in the script and adjust them to your needs. Now save and run the script. You now may go and get some coffee, tea or whatever you prefer because this will take some time.
  1. As of the time of this writing the mingw-runtime-3.10-20060909-1 package still contains a broken math.h. Get [this patch to math.h], store it under the name mingw-runtime-3.10-20060909-1.math_patch in the same directory as the above shell script and it will be applied during build automatically. Be warned that possibly the <TAB> character in the patch do not transmit correctly :-( If anyone knows how to deal with that please drop me a note and I'll fix it.

If nothing goes wrong you'll end up with a complete toolset in $PREFIX.

For an alternate, but entirely equivalent, way to build a Win32 cross-compiler for Linux, Igor Mikolic-Torreira has created a script, BuildMingwCross, which is even more configurable and does all the downloading for you. To use Igor's script, just copy it from the BuildMingwCross page, save it as a shell script on your Linux system, edit the settings to your needs, and run it.

Note that you should update the filenames of archives downloaded by the script to reflect the current versions of MinGW components (this script is not updated whenever MinGW components are updated).



I'd already struggled and succeeded to build a Win32 cross-compiler on Linux with C and C++ for 'i586-pc-mingw32-g++ (GCC) 4.3.0 20070121 (experimental)', before I came across this Wiki page. I don't know whether the above 2 shell scripts will work with gcc 4; they certainly look more sophisticated than my method. If you're trying gcc 4, and the above doesn't work for you, you might want to try my method: see the first post of this this thread

And if the above methods work for gcc 4, then you can delete this note of mine.

-- ChipHog


Success with GCC 4.1.2

Here's summary of creating Win32 cross-compiler on Gentoo Linux and using it to build 3D application with CrystalSpace 3D 1.1 SDK and wxWidgets wxGTK-2.8.4 (openGL version) for Windows XP. The used code-base compiles successfully on linux (using
Gentoo's standard gcc-4.1.2) and on windows XP (tested with both MS VisualStudio 8 and with MinGW gcc-4.1.2 installation on windows) and produces working
executables on both platforms. The cross-building described below did not require any changes to the code.

Build system: 2.6.22-gentoo-r5 #2 SMP, i686 AMD Opteron(tm) Processor 250 AuthenticAMD GNU/Linux

I had no root access to local system, therefore everything, including the cross-compiler, was configured to build and install into non-standard directories.

I used Igor Mikolic-Torreira's handy shell script with following modifications to create gcc-4.1.2 cross-compiler on Gentoo.

< PREFIX=/scratch/mingw32/
> PREFIX=/usr/local/mingw32
< GNU_URL=""
< GCC_CORE_ARCHIVE="gcc-core-4.1.2.tar.gz"
< GCC_GPP_ARCHIVE="gcc-g++-4.1.2.tar.gz"
> GCC_CORE_ARCHIVE="gcc-core-3.4.2-20040916-1-src.tar.gz"
> GCC_GPP_ARCHIVE="gcc-g++-3.4.2-20040916-1-src.tar.gz"
> GCC_G77_ARCHIVE="gcc-g77-3.4.2-20040916-1-src.tar.gz"
> GCC_OBJC_ARCHIVE="gcc-objc-3.4.2-20040916-1-src.tar.gz"
> GCC_JAVA_ARCHIVE="gcc-java-3.4.2-20040916-1-src.tar.gz"
< #BINUTILS_ARCHIVE="binutils-2.15.91-20040904-1-src.tar.gz"
< BINUTILS_ARCHIVE="binutils-2.17.50-20070129-1-src.tar.gz"
> BINUTILS_ARCHIVE="binutils-2.15.91-20040904-1-src.tar.gz"

> MINGW_ARCHIVE="mingw-runtime-3.7.tar.gz"
< MINGW_ARCHIVE="mingw-runtime-3.13.tar.gz"
< W32API_ARCHIVE="w32api-3.9.tar.gz"
> W32API_ARCHIVE="w32api-3.2.tar.gz"
< download_file "$GCC_CORE_ARCHIVE" "$GNU_URL"
> download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
< download_file "$GCC_GPP_ARCHIVE" "$GNU_URL"
> download_file "$GCC_GPP_ARCHIVE" "$MINGW_URL"


wxWidgets was configured with

.../wx/wxGTK-2.8.4/configure --prefix <my_build_dir> --host=mingw32 --target=mingw32 CXXFLAGS=-I/scratch/mingw32/mingw32/include/ "LDFLAGS=-L/scratch/mingw32/mingw32/lib/ -s -O" CXX=/scratch/mingw32/bin/mingw32-g++ --with-z=/scratch/mingw32/mingw32/ --with-opengl --enable-stl --disable-sound

CrystalSpace/Our 3D application was then configured with

.../configure --prefix <my_build_dir> --host=mingw32 --target=mingw32 "CXXFLAGS=-I/scratch/mingw32/mingw32/include/ -I<wxdir>/wx/wxGTK-2.8.4/include/" "LDFLAGS=-L/scratch/mingw32/mingw32/lib/ -lglut32 -s -O" CXX=/scratch/mingw32/bin/mingw32-g++ --with-z=/scratch/mingw32/mingw32/ --without-CEGUI --without-asound --without-jackasyn --without-java --without-perl --without-python --with-wx

Only problems I encountered during the build were weird mingw32-windres errors when using --use-temp-file option.
Also, mingw32-windres didn't like rc files with windows line-feed/carriage-returns (shown as ^M in vi) either.
To solve these I had to tweak my jam files not to use --use-temp-file option and clean rc files with dos2unix.

There were, however, lots of following warnings during the compilation


 warning: visibility attribute not supported in this configuration; ignored



warning: type attributes are honored only at type definition

I just ignored these on this stage.

Hope this helps people struggling with the MinGW cross-compiler or wondering if it works with GCC4.

-- JulleJuntunen


Updating shell script for bash

If you get an error at line 172 and or 200 in the shell script, it may be that your /bin/sh is really bash or dash.

Line 172: delete the word "function" from "function run_error () {"

Line 200: bash requires spaces both sides of every semi-colon in the for loop

   change "for (( ; $pass_count; pass_count=$pass_count-1 ))"

   to      "for (( ; $pass_count ; pass_count=$pass_count-1 ))

-- WollonGong (AU)

Site Status

Site maintenance performed successfully, site online Dec 13th 12:00 AM Eastern.