====== A Cross-Compiler Targeting mingw32 ====== * Author: [[../coleman_kane|Coleman Kane]] * Last Updated: **26 Jul 2007** This document covers my experience, and my solution to building a **GCC 4.2.0** cross compiler for the **mingw32** target. I have used this exact same process to build the cross-compiler on the following hosts: * amd64-unknown-freebsd7 (FreeBSD 7-CURRENT) * powerpc64-unknown-linux-gnu (Gentoo) * i686-pc-linux-gnu (Gentoo) * powerpc-apple-darwin8.10.0 (Mac OS X 10.4.10) Due to the heterogeneity above, I think that it is likely that these instructions will work for most any POSIX-like platform. Note that due to this work, I've been helping get the requisite **devel/mingw32-* ** ports updated to the latest releases, including GCC 4.2.0 and binutils. Visit my [[:freebsd|FreeBSD]] section for the port tarballs. ===== Introduction ===== Searching around on the net, there is a lot of outdated information discussing building a cross-compilation environment using [[http://gcc.gnu.org/|GCC]]. In fact, much of this information harks back to a day when such a feat was a huge undertaking. Due to the demand for this configuration, the GCC people have made this work immensely easier, yet documentation is sparse around the Internet. Hopefully, I can help out another poor soul who delves down this path. Perhaps, these instructions will help out in the more generic cases as well (other targets than mingw32). ===== Gather the Necessary Resources ===== You will need the following software packages, in addition to a native compiler for your host operating system: * GNU binutils 2.17.50 (available from snapshots [[ftp://sourceware.org/pub/binutils/snapshots]]) * GNU GCC (available from mirrors [[http://gcc.gnu.org/mirrors.html]]) * MingW32 Runtime Files (From [[http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=82722&release_id=158845|Sourceforge]]) * w32api - Win32 API Runtime (non-mingw, available from [[http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=82721&release_id=158847|Sourceforge]]) Download all of these to your home directory. Don't immediately unpack all of them, yet. Create two directories: mkdir ~/mingw32-cross-src mkdir ~/mingw32-cross-build The folder **~/mingw32-cross-src** will hold all of the sources for the cross-build software, while the building of the tools will be performed in **~/mingw32-cross-build**. This division is commonplace when working with the GNU compiler collection, and is supported by **GNU Autotools**. ===== Build and Install GNU binutils ===== You will first need to build the **GNU binutils** cross-utilities for your cross-building environment. This package contains the assembler for the target architecture, as well as numerous utilities that allow you to view and operate upon the binaries produced for the target architecture (such as ''nm'', ''ranlib'', etc...) Change to the **src** directory, and unpack the **binutils** package: cd ~/mingw32-cross-src tar xjf ~/binutils-2.17.50.tar.bz2 Next, change to the **build** directory, and create a new place for **binutils** to be built in (named **binutils-2.17.50-build**), enter that directory: cd ~/mingw32-cross-build mkdir binutils-2.17.50-build cd binutils-2.17.50-build Next, you will have to use **configure** to create your cross-build binutils. Supply the command-line below, using the options I have provided. You may add more, if you like, but I have likely not tested them. If you find placese where these options don't work, or other options that do work, please let me know and I will update this page. ../../mingw32-cross-src/binutils-2.17.50/configure --target=mingw32 --prefix=/usr/local Finally, build it and install it (you may need root privileges): make ... magic happens ... make install You should now have a **/usr/local/mingw32** directory, as well as an **mingw32-as** assembler installed in the **/usr/local/bin** directory. ===== Install Target-Dependent Headers, Libraries, and Runtime ===== In order to build for MinGW32, you will need the headers and libraries on your host system. The **mingw-runtime** and the **w32api** packages, which you downloaded above, provide all of this. After completing the previous step, to build and install **GNU binutils**, you should now have a directory located at **/usr/local/mingw32** that contains all of the compiler tools necessary for the MinGW32 platform. You need to put the contents of **mingw-runtime** and **w32api** into here to get it to work. This was probably the most confusing for me, as I went through putting these files in all kinds of other places before I figured this one out. Enter the directory: cd /usr/local/mingw32 Unpack the distributions: tar xzf ~/mingw-runtime-1.12.tar.gz tar xzf ~/w32api-3.9.tar.gz You will now have the necessary run-time headers and libraries to build your C compiler. ===== Build GCC ===== Now you are ready to build and install the GNU Compiler Collection. Note that in order to build the **FORTRAN95** compiler, //gfortran//, you will need the latest versions of [[http://gmplib.org/|GMP]] and [[http://www.mpfr.org/|MPFR]] installed, native to your host architecture. Having a mingw32-built version of these is not necessary. Change to the GCC **build** directory: cd ~/mingw32-cross-build/gcc-4.2.0-build Run **configure** providing the desired options: ../../mingw32-cross-src/gcc-4.2.0/configure --prefix=/usr/local --enable-languages=c,c++,fortran,java,objc,obj-c++ --target=mingw32 --with-gmp=/usr/local --with-mpfr=/usr/local --without-x --disable-win32-registry --enable-threads --enable-hash-synchronization --enable-sjlj-exceptions This will fully-configure GCC for your target platform, and support the following languages: C, C++, FORTRAN95, Objective-C, and Objective-C++. You may change the language list above, as necessary. According to reading on the Internet, **--enable-sjlj-exceptions** will preempt GCC trying to use DWARF2 exception handling. DWARF2 exception handling breaks under Win32 as I have been told. I have not had a chance to test this out for myself, however. Next, you will want to build and install the cross compiler(s): make ... more magic happens again ... make install You should now have a C++ compiler at **/usr/local/bin/mingw32-g++** as well as other compilers that you chose installed to the same directory. ===== Congratulations ===== Congratulations, you now have everything you need to start building and compiling binaries targeted for MinGW32. One recommendation that I have is to create a new PREFIX directory into which you will build and install software and libraries for your target architecture. This will help keep them from polluting the system-dependent directories in **/usr/local/mingw32** and also keep them from colliding with similar files on your host system. ====== Discussion ====== Please post further discussion here, and share your thoughts! ~~DISCUSSION~~