Building gcc 4 for Windows 2000

March 12, 2007

Pre-built binaries of gcc 3 have long been available from MinGW and Cygwin, but I know of no source for a pre-built gcc 4 port for Windows 2000. I wanted to experiment with the TR1 functionality in gcc 4, so on October 30, 2006, I posted to the newsgroups gnu.gcc.help, gnu.g++.help, and comp.lang.c++ for suggestions on what to do. Thomas Tutone offered this idea:

Your best bet: Download DJGPP from www.delorie.com. Yes, it's old school, and it lacks the Windows headers, but it will run in a Windows DOS box, which is probably the way you use minGW anyway. DJGPP upgraded to gcc 4.10 some time ago.

I did this, and quick tests showed that it seemed to work, but they also suggested that compilations would be quite slow. I then got intriguied by this posting by FĂ–LDY Lajos:

There is a mini-howto for building GCC 4.1.1 for Windows (including W2K): http://gcc.gnu.org/ml/gcc-help/2006-09/msg00000.html

I'd never built gcc before, so I decided to give it a try. I ultimately succeeded, and what follows is an annotated version of the mini-howto referenced above; the howto itself was written by Marcelo Slomp -- thanks Marcelo! My annotations primarily point out places where I made mistakes. My hope is that others will be able to avoid my errors.

Annotated Build Instructions for gcc 4 for win32

Text in code font is from Slomp's howto. My annotations (such as this) are in italics.

   I've written a mini-howto, containing instructions on how to build the
   latest gcc (currently v.4.1.1) from scratch under win32, using the
   msys/mingw environment.
   
   Readme.1st: apologize my imperfect english :/
   
   I   - Setting up the MSYS envirnoment
   II  - Installing pre-built gcc binaries
   III - Creating the gcc host environment
   IV  - Building and installing gcc
   V   - Adding other build tools
   VI  - Have fun
   
   
   I - SETTING UP THE MSYS ENVIRONMENT
   The first thing we'll need is a fully functional msys environment. You can
   do this quickly by just downloading and installing the msys binaries
   package.
   
   To keep a clean understanding, I'll assume you installed msys in the c:\
   root.
   This way, msys is located at: c:\msys
   
   
   II - INSTALLING PRE-BUILT GCC BINARIES
   
   First we download and install a pre-built version of gcc 3.  This will be
   used later to compile gcc 4.  Once that's done, the gcc 3 installation can
   be deleted (as noted below).
   
   Now that msys is already installed, we need some version of gcc itself in
   order we can build the latest version. This way, download the following
   binary packages:
   
   from GNUWin32 project:
   bison
   flex
   libiconv
   libintl
   
   from MinGW project:
   win32api
   mingw-runtime
   binutils
   gcc-core
   
   To install its, create a new directory called 'gcc' or any other you want in
   the c:\ root. Then extract all files above inside this new directory.
   However, for libiconv and libintl, only extract the dlls inside the
   package's ./bin subdir and copy its to the c:\gcc\bin directory.
   
   When extracting, note that some downloads are .zip files and some are
   .tar.gz files.  Be sure your archive extracter puts all the files in the
   right places.  I used WinZip.  WinZip put all the .zip file contents in the
   right place without my paying much attention, but when opening the .tar.gz
   files, it switched working directories to my TEMP directory, so the first
   time I did this I ended up extracting the contents of the .tar.gz files to
   the wrong places.
   
   Important: after extraction, go to c:\gcc\bin and delete m4.exe (msys uses a
   specific m4 build, included with msys package).
   
   After this, add the c:\gcc\bin directory to the PATH environment variable.
   
   The final installation location of gcc 4 is under mingw (see below), so
   when gcc 4 has been built, the search path needs to include that location,
   not the one for gcc 3, which is what's being referenced here.  I tried to
   cut a corner by setting the PATH at this point only in the DOS box I
   happened to be using, but the PATH that needs to be set is in the msys
   command window, not a DOS box!  So (1) don't use a DOS box for any of the
   following, use a msys command window, and (2) make sure that command window
   has the bin directory for gcc3 in its search path.  To do this only in the
   msys window, you'll need to use a Unix sh command.  I did this:
   
     # PATH=/c/gcc/bin:$PATH
   
   (The "#" is the sh (Unix shell) command prompt.  If you've never used Unix
   before, you may want to find somebody who has to help you out with what
   follows.  Don't worry, it's not that hard.  My last stint with Unix was
   some 13 years ago, and I managed get things to work.)
   
   Notice that gcc-g++ package is not used here, because is not necessary a c++
   compiler to build gcc.
   
   You can check if it's all right by opening msys, then typing:
   # gcc -v
   This should show some info about the pre-built gcc.
   
   It will mention something like gcc version 3.4.5.
   
   If working, we can go to the next step.
   
   
   III - CREATING THE GCC HOST ENVIRONMENT
   In order to build gcc, we need setup a host directory that will receive the
   new gcc binaries and files.
   This should be done before the build itself, because this host directory and
   files are used during the build process.
   
   Supposing you installed msys and the pre-built gcc under c:\, you have now
   the following directory structure:
   
   c:\
    |---folder_1
    |---folder_2
    |---gcc      <- here you installed the pre-built gcc
    |---msys      <- msys environment is installed here
    |    |---bin
    |    |---doc
    |    |---etc
    |    |---home
    |    |---mingw      <- built gcc will be installed here
    |---folder_n
   
   
   Now, you'll create the host environment that will receive the new gcc build
   (c:\msys\mingw).
   To do this, you need install the mingw-runtime, binutils and win32 api. Just
   extract the mingw-runtime and w32api packages and extract its into the
   c:\msys\mingw folder. For binutils, you can either, use the binutils
   binaries already downloaded or build it youself.
   For the first option, just extract the binutils binary package into the
   c:\msys\mingw folder.
   If you want to build your own binutils binaries, please read in the next
   chapter about how to do this.
   
   
   IV - BUILDING AND INSTALLING GCC
   In msys environment, create a work directory, where the sources and
   temporary build files will be placed:
   # cd /
   # mkdir gcc-work && cd gcc-work
   # mkdir {build,source}
   
   The lines beginning with "#" are the actual commands to type to the msys
   shell.  To avoid mistyping things (which I did by mistaking the curly
   brackets in the third line with parentheses), copy the lines and paste them
   into the msys window.  To copy the clipboard into an msys command window,
   hold down the shift key while left-clicking the mouse.  
   
   *** Building binutils
   If you want to build binutils from source, here are the instructions.
   Otherwise, jump this section.
   
   I used the pre-built binaries and thus skipped this section.  The next
   thing I did was jump to "Building gcc".
   
   
   Download the source package
   copy/move it to the source folder (in the work directory - /gcc-work/source)
   extract it:
   # cd source
   # tar jxf binutils-2.17.tar.bz2
   
   go to the build directory and create a temporary directory where binutils
   will be built:
   # cd ../build
   # mkdir binutils
   # cd binutils
   
   configure, compile and install binutils in the /mingw directory:
   # ../../source/binutils-2.17/configure --prefix=/mingw --host=mingw32
   --target=mingw32 --program-prefix=""
   --with-lib-path=/mingw/mingw32/lib:/mingw/lib:/usr/local/lib:/lib:/usr/lib
   --with-gcc --with-gnu-as --with-gnu-ld --disable-nls --disable-shared
   # make  CFLAGS="-O2 -D__USE_CRTIMP -fno-exceptions" LDFLAGS=-s
   
   flags are self-explanatory, but you can get info about configuration flags
   by running the configure --help command, but is important to pay attention
   in the --with-lib-path flag. it determinate where utils such ld will find
   for libraries. as we are installing under /mingw directory, the first 2
   paths must point to this prefix. if you want to change the prefix for some
   reason, do not forgot to change this! other paths are required when
   searching for libs into the msys environment (if you plan to use msys
   later).
   
   this is a time to a coffe or a beer... :)
   the build process will take several minutes.
   
   after this, just install it as the usual:
   # make install
   
   
   *** Building gcc
   The most wanted moment has come: the gcc build itself. don't worry, nothing
   surreal or cabalistic here. :)
   First, download the gcc source packages - here, only c and c++ languages
   will be installed, but you can obviously download other packages and modify
   the command lines if you want to install other languages, such ada and java.
   Read about gcc configuration flags.
   
   Link to gcc-core.
   Link to gcc-g++.
   
   copy/move its to the source directory, then extract:
   # cd /gcc-work/source
   # tar jxf gcc-core-4.1.1.tar.bz2
   # tar jxf gcc-g++-4.1.1.tar.bz2
   
   go to the build directory and create a temporary directory where gcc will be
   built:
   # cd ../build
   # mkdir gcc
   # cd gcc
   
   configuring:
   # ../../source/gcc-4.1.1/configure --prefix=/mingw --host=mingw32
   --target=mingw32 --program-prefix="" --with-as=/mingw/bin/as.exe
   --with-ld=/mingw/bin/ld.exe --with-gcc --with-gnu-ld --with-gnu-as
   --enable-threads --disable-nls --enable-languages=c,c++
   --disable-win32-registry --disable-shared --without-x --enable-interpreter
   --enable-hash-synchronization --enable-libstdcxx-debug
   
   Copy and paste is definitely your friend for this command and the next
   one.  Just be sure to edit out the line breaks after making the copy.
   Everything from the "../.." at the beginning of the command to the "-debug"
   at the end must be on a single line.
   
   # make CFLAGS="-O2 -fomit-frame-pointer" CXXFLAGS="-mthreads
   -fno-omit-frame-pointer -O2" LDFLAGS=-s bootstrap2
   the bootstrap process is recommended in the gcc documentation, and will
   produce a compiler more optimized than other that was not built in this way.
   during the bootstrap, a compiler will be generated, that will compile
   another compiler, that will compile another compiler and so on. the
   bootstrap2 command instructs to generate 2 pre-compilers before the final
   version.
   obviously this process consumes much more build time, but the final result
   is better. you can supress the bootstrap2 target in the make line, if you
   don't want a bootstrapped compiler.
   
   now, relax. drink a coffe, or lots of. this operation will take some long
   time to complete.
   
   On my machine (a five-year-old IBM laptop), this step took several hours.
   
   after this, the happiness is complete: install your brand new gcc :)
   # make install
   
   your fresh new gcc build is now installed and ready to use. you can then
   delete the work directory, as well the pre-built gcc directory. don't forgot
   to add to the PATH variable the new gcc's bin directory.
   
   This is as far as I went, so I have no comments on the rest of the howto.
   
   V - ADDING OTHER BUILD TOOLS
   Now that the new gcc binaries are already installed, we can add other
   development tools to the gcc box. here, presenting gdb and make.
   
   - installing gdb:
   download the precompiled binaries of gdb:
   Link to gdb 6.3-2.  
   Install the package in the c:\msys\mingw directory
   
   i have built gdb 6.5 with success, however it does not have native support
   for the pe executables. this way, the best alternative is to install the
   binary package above.
   
   - installing make:
   notice that 'make' is already included with the msys package. however, if
   you do not plain to work with msys later but want to use makefiles, you need
   your own make binary.
    
   you can install the precompiled binary package by extracting the following
   package into the c:\msys\mingw folder:
   http://prdownloads.sourceforge.net/mingw/mingw32-make-3.81-1.tar.gz?download
   
   or build it youself. to do this, download the source package:
   make 3.81 source: http://ftp.gnu.org/gnu/make/make-3.81.tar.bz2
   copy it to the source folder in msys, then extract it:
   # cd /gcc-work/source
   # tar jxf make-3.81.tar.bz2
   
   go to the /gcc-work/build and create a make's build directory:
   # cd ../build && mkdir make
   # cd make
   
   build and install it:
   # ../../source/make-3.81/configure --prefix=/mingw --target=mingw32
   --program-prefix="" --disable-nls --disable-dependency-tracking
   # make CFLAGS=-O2 LDFLAGS=-s
   # make install
   
   
   VI - HAVE FUN
   As the title says, have fun with your latest version of gcc under win32!
   Builds made with success in WinXP and Win2000, but these instructions should
   work in other Win32  systems, such Win98/ME/NT/2003/Vista.
   Recent snapshots for gcc 4.1 were built with success using the same approach
   and steps.