HOWTO Build MinGW as a Cross Compiler for Win32 Development on GNU Linux (Obsolete Information)

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.

See here for a discussion on this:
http://thread.gmane.org/gmane.comp.gnu.mingw.user/16318, and here to see when I realised this problem:
http://thread.gmane.org/gmane.comp.gnu.mingw.user/16778

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.

  1. Download at least the following (or newer) packages from the MinGW download page:
  2. You may also like to add any of the following additional (optional) languages; (note that to activate them, you will have to modify the provided shell script yourself).
  3. 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.
  4. 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.

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 a gcc-4.1.2 cross-compiler on Gentoo.

61c61
< PREFIX=/scratch/mingw32/
---
> PREFIX=/usr/local/mingw32
76c76
< GNU_URL="ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-4.1.2"
---
>
81,85c81,85
< GCC_CORE_ARCHIVE="gcc-core-4.1.2.tar.gz"
< GCC_GPP_ARCHIVE="gcc-g++-4.1.2.tar.gz"
< GCC_G77_ARCHIVE=""
< GCC_OBJC_ARCHIVE=""
< GCC_JAVA_ARCHIVE=""
---
> 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"
89,90c89,91
< #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"
92,93c93
< MINGW_ARCHIVE="mingw-runtime-3.13.tar.gz"
< W32API_ARCHIVE="w32api-3.9.tar.gz"
---
> W32API_ARCHIVE="w32api-3.2.tar.gz"
155c155
<         download_file "$GCC_CORE_ARCHIVE" "$GNU_URL"
---
>         download_file "$GCC_CORE_ARCHIVE" "$MINGW_URL"
157c157
<                 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

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

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

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

-- WollonGong (AU)

Site Status

Site maintenance completed May 25th, 2012 at 12:38 UTC