MinGW from Source - How To Build the GNU Compiler Suite on Windows

I am no longer using the official MinGW version of the compiler suite or the various forks available on the Internet for my own development needs. Instead, I now using gcc directly from the GNU web site in combination with a runtime library of my choice with less stringent licensing. I will be making copies of my particular gcc/g++ builds including patches needed to compile available in the near future. If you want a working GNU compiler for Windows or are interested in methods/patches used to build a more recent version of the GNU compiler, please check out my LM BLD project.

There are many ways to build MinGW (the GNU compiler suite) on Windows from source code. So what's the point of building MinGW from source if you need MinGW to do so and it takes several hours? You can customize the configure and make settings for your particular system and needs. You can also edit, modify or use a different runtime library if you prefer. See the information in my MinGW FAQ for more details on why using a runtime library of choice may be a very desirable option.

I was asked by a reader of my MinGW FAQ how to build MinGW from scratch. This page explained the steps I used at the time. I scripted the technique using the LM BLD build scripts so that it can be done in a repeatable and automated fashion. However, I just covered the highlights from the scripts below. As mentioned, this page contains dated material. Rather than continuing to update it and several other patches to other projects that I've shared at several locations (at this site and across the Internet), I've consolidated my work through the LM BLD project. Please see the LM BLD project for my latest build scripts and build techniques for a variety of Open Source projects including gcc. While I hope to continue to work with the gcc compiler suite, I'm planning on updating the runtime library code to support development needs that I can no longer support by using other popular Windows compilers currentlly available. I also feel that just having a compiler isn't sufficient. So, one goal of the LM BLD project is to offer patches and build information to successfully build useful Open Source libraries, utilites, tools and programs with that compiler. If you'd like to help, feel free to contact me. If you'd like to show your support for the project and feel as I do that there's a need for it, it would be encouraging to hear from you.

I will warn you that building the GNU compiler suite can be a lengthy process. It took over 4 hours on my fastest machine and that was just to build the C (gcc) and C++ (g++) compilers. One must also make sure that no virus scanners are running during the gcc build. They may interfere with the build process. In order to build the GNU compiler suite, you need a working GNU compiler. The situation is similar to what comes first, the chicken or the egg? If you have a working GNU compiler on another system, you can use it to build a MinGW cross-compiler and then eventually build a Windows based compiler. I won't be going into details on building a cross-compiler at this point in time. I recommend checking out the MXE project if you want to look at scripts to build a cross-compiler. The steps below are for building the MinGW compiler assuming you already have an ABI compatible MinGW compiler and msys installed on your system. At some point, I hope to replace msys with native Windows alternatives as part of the LM BLD project.


You'll need to download the libraries and compiler mentioned below. Before some asks why there are no links to download these files directly, please keep in mind that the MinGW project which maintains some of these files tends to move their locations often. The links would probably be useless soon after they're published. Please use your favorite search engine to find the listed files. It's really not that difficult to do. I recommend you use the same version numbers. If you change version numbers, different patches (not listed here) will typically apply. When you're ready to build, do so from within msys. I set up a few environment variables before getting started:

export PREFIX=/usr/local
export DESTDIR=[directory to install to]

I typically set DESTDIR to a temporary location and then tar and compress the results. I use the LM BLD build tools to do the actual install to the proper directories after each library or program is built. If you want to skip the extra step, you can just leave DESTDIR set to a null string and the libraries and compiler should install to /usr/local.

I also want the libraries and include files from the projects I'm working with to be found before the other versions on the system. So, I set the CFLAGS and LDFLAGS environment variables as well. When working with C++ projects, I set the CXXFLAGS the same way. If I need the preprocessor to find the proper directories too, I set CPPFLAGS.

export CFLAGS="-I/usr/local/include $CFLAGS"
export LDFLAGS="-L/usr/local/lib $LDFLAGS"

The official MinGW typically installs to the \mingw directory. My build installs mingw in the /usr/local hierarchy. This doesn't interfere with the version of MinGW already installed and follows the FHS convention for locally built applications and libraries. If you change the PREFIX to another location when building, you may need to modify other steps to make this work properly.

Decompress and untar the files. Go to the top level where the files decompressed. For instance, with binutils, go to the binutils-2.23.2 subdirectory that was created when untar was used on the file. Perform the configure and make commands as needed and described below for each library or application. I've read some posts that recommend not building in the same directory as the source. I've had no trouble building applications and libraries this way and do typically build from the source directory. The one exception is when gcc is built. I change to a build directory. The official instructions on building the GNU compiler suite recommend doing so and do not guarantee the compiler will build if tried within the source directory. If you have any problems or issues building the GNU compiler suite, I highly recommend, you check the GNU compiler's web site for more detailed instructions or further help.


As mentioned, some patching was required. I've included the patches I've used at the bottom of this page. Save each to a file called patch.diff in the appropriate directory of the library or program you're building. Apply the patches as indicated in the steps below. It would be nice if the compiler suite and corresponding libraries built as is with no patching required. However, finding the appropriate patches needed plus finding the command line options to configure and make them work are the most difficult parts of building the GNU compiler suite. If no patches were required and the same settings worked on each version of the GNU compiler suite, it would be so much easier to upgrade to newer versions. Unfortunately, it's easier to use older versions that are mostly debugged than to try for the latest and greatest version of the gnu compiler suite and have to debug and patch the compiler on one's own.



Build gettext (libintl). There's a circular dependency between GNU's gettext and iconv on Windows. I use bsdgettext from BSD's Citrus Project with msgfmt, msgmerge, xgettext from gettext-tiny instead. It's less bloated than the GNU version and avoids the circular dependency. I will be using these alternatives with the LM BLD project. You can also use the gettext binaries from the GNU web site or the sites of various MinGW forks if you don't wish to build from source.


For libiconv-1.14.tar.gz, the following build steps were used:

./configure --prefix=$PREFIX 
make install DESTDIR=$DESTDIR


For pthreads-w32-2-9-1-release.tar.gz, the following build steps were used:

make clean GC GCE
make install DESTDIR=$DESTDIR
mkdir -p $DESTDIR$PREFIX/lib
cp *.a $DESTDIR$PREFIX/lib
mkdir -p $DESTDIR$PREFIX/bin
cp *.dll $DESTDIR$PREFIX/bin
mkdir -p $DESTDIR$PREFIX/include
cp pthread.h $DESTDIR$PREFIX/include
cp sched.h $DESTDIR$PREFIX/include
cp semaphore.h $DESTDIR$PREFIX/include
cp $DESTDIR$PREFIX/lib/libpthreadGC2.a $DESTDIR$PREFIX/lib/libpthread.a

Libraries needed during the build

One can build gmp, mpfr and mpc separately before building the gnu compiler suite or place the source in directories with those same names alongside the compiler source and the libraries will be built by the compiler scripts. I chose to build them all separately since the gnu compiler takes a long time to build on its own. One can also build libgomp, but it isn't required for the C/C++ compilers, only the GNU Fortran compiler.


For gmp-5.1.1.tar.xz, the following build steps were used:

./configure --prefix=$PREFIX --disable-static --enable-shared --enable-cxx --enable-assembly ABI=32
make install DESTDIR=$DESTDIR


For mpfr-3.1.2.tar.xz, the following build steps were used:

./configure --prefix=$PREFIX --enable-shared --enable-thread-safe
make install DESTDIR=$DESTDIR


For mpc-1.0.1.tar.gz, the following build steps were used:

./configure --prefix=$PREFIX --enable-shared
make install DESTDIR=$DESTDIR


I'm using w32api-3.17-2-mingw32-src.tar.lzma. This is the WIN32 interface for the MinGW runtime. The following build steps were used:

./configure --prefix=$PREFIX 
make install DESTDIR=$DESTDIR


I'm using mingwrt-3.20-2-mingw32-src.tar.lzma. This is the last version of the MinGW runtime that is in the public domain. Newer versions use different licenses. For more information, on MinGW runtime libraries, see my MinGW FAQ. The following build steps were used:

patch -p2  < patch.diff 
sed -i.bk0 -e "s|doc/runtime|share/doc/mingwrt|" Makefile 
./configure --prefix=$PREFIX 
make install DESTDIR=$DESTDIR


For binutils-2.23.2.tar.bz2, the following build steps were used:

./configure --prefix=$PREFIX --disable-nls --disable-shared --disable-werror
make install DESTDIR=$DESTDIR

Note: If you have dl (aka libdl or sometimes called dlfcn) installed, you may need to uninstall it before building binutils or supply information to link to it such as LIBS="$LIBS `pkg-config dl --libs` or LIBS="$LIBS -ldl`.


For gcc-4.7.2.tar.bz2, the following build steps were used:

mkdir ../bldgcc
cd ../bldgcc
../gcc-4.7.2/configure --prefix=$PREFIX $CONFIG_OPTIONS --disable-rpath --disable-nls --enable-languages=c,c++ --with-system-zlib --enable-version-specific-runtime-libs --disable-libgomp --disable-libmudflap --disable-sjlj-exceptions --enable-checking=release --disable-win32-registry --enable-threads=win32 --disable-build-poststage1-with-cxx --with-stage1-ldflags=/usr/local/lib --with-boot-ldflags=/usr/local/lib --enable-shared
patch -p2 $PATCH_OPTIONS <  patch.diff 


The patches described below were needed to get the GNU compiler suite to build without errors.


For mingwrt-3.20-2-mingw32-src.tar.lzma, place the following in a file named patch.diff:

diff -Naurp src/mingwrt-3.20-mingw32/mingwex/gdtoa/strtodg.c tmp/mingwrt-3.20-mingw32/mingwex/gdtoa/strtodg.c
--- src/mingwrt-3.20-mingw32/mingwex/gdtoa/strtodg.c	2009-07-12 18:44:37 -0400
+++ tmp/mingwrt-3.20-mingw32/mingwex/gdtoa/strtodg.c	
@@ -29,6 +29,10 @@ THIS SOFTWARE.
 /* Please send bug reports to David M. Gay (dmg at acm dot org,
  * with " at " changed at "@" and " dot " changed to ".").	*/
+#ifndef DBL_DIG
+#define DBL_DIG __DBL_DIG__
 #include "gdtoaimp.h"
 #ifdef USE_LOCALE
diff -Naurp src/mingwrt-3.20-mingw32/mingwex/math/hypotl.c tmp/mingwrt-3.20-mingw32/mingwex/math/hypotl.c
--- src/mingwrt-3.20-mingw32/mingwex/math/hypotl.c	2002-09-01 23:00:37 -0400
+++ tmp/mingwrt-3.20-mingw32/mingwex/math/hypotl.c	
@@ -2,6 +2,13 @@
 #include <float.h>
 #include <errno.h>
+#ifndef LDBL_MAX_EXP
+#ifndef LDBL_MIN_EXP
 This implementation is based largely on Cephes library
 function cabsl (cmplxl.c), which bears the following notice:


For gcc-4.7.2.tar.bz2, place the following in a file named patch.diff:

diff -Naurp src/gcc-4.7.2/gcc/ginclude/float.h tmp/gcc-4.7.2/gcc/ginclude/float.h
--- src/gcc-4.7.2/gcc/ginclude/float.h	2011-12-20 15:44:13 -0500
+++ tmp/gcc-4.7.2/gcc/ginclude/float.h
@@ -276,3 +276,9 @@ see the files COPYING3 and COPYING.RUNTI
 #endif /* __STDC_WANT_DEC_FP__ */
 #endif /* _FLOAT_H___ */
+#ifdef __MINGW32__
+#ifndef _MINGW_FLOAT_H_


Hopefully with the instructions above, you'll be able to replicate building MinGW on your own system. If you have further questions, please check with the web site for the library or program you're interested in. As mentioned, there is more detailed information on building the GNU compiler suite at the GCC web site. The GCC site also covers issues to watch out for or avoid.

Once you have your MinGW compiler built from scratch, you may want to check out my MinGW FAQ for usage examples and other tips.


To the main page.


Validate XHTML

The information on these pages is copyrighted by the author with all rights reserved. Reproduction of anything without the author's permission is in violation of copyright laws.
All original material is copyrighted:
(c) Copyright 2013, 2015 by Laura Michaels
All Rights Reserved
Last Update: 20150927