Revision of HOWTO Build MinGW as a Cross Compiler for Win32 Development on GNU Linux (Obsolete Information) from 2008, September 15 - 16:08

Posted August 8th, 2008 by earnie

Minimalist GNU for Win32 (updated for MinGW gcc 3.4.5)

NOTE:

Much of the information presented on this page is obsolete; updated information may be found here.

WARNING:

This will NOT generate a compiler that is compatible with the standard MinGW.org 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.

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

Addition:

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

enable_sjlj_exceptions=0

early in the script. Setting this variable to 1 will make the cross compiler 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 it is with the author of these lines; building Qt under native Win32 with stock MinGW.org gcc using SJLJ and then using that Qt to create applications under Linux via cross-compile for windows with a MinGW gcc using DW2), there is no reason to not use DW2 EH.

How to Build a Win32 Cross Compiler for Linux

To compile and link your Win32 executables under Linux you need a cross compiler. Luckily MinGW already provides all that is required. To actually get hold of such a cross 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 cross compiler from the sources.

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 have to 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.

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 automatically, during the build. 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, Build MinGW Cross, which is even more configurable and does all the downloading for you. To use Igor's script, just copy it from the Build MinGW Cross 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).

Information below this point has not been approved by the original page author

GCC 4

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 http://forums.newdoom.com/showthread.php?t=32297

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 a summary of creating a 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 code-base compiles successfully on GNU/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.

The only problems I encountered during the build were weird mingw32-windres errors when using the --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 the --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

and

warning: type attributes are honored only at type definition

I just ignored these, at 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