How to install a newer version of GCC

The Cygwin 1.7.x devel area offers an older version of GCC (3.4.4-999), which is outdated for many purposes. (GCC 4.5.3-4 is also offered in the gcc4 package, but the Cygwin 1.7.x devel area does not offer GCC 4.6 or later). This article describes how to upgrade your Cygwin GCC installation to a more recent version (e.g., 4.6.1).

Alternatively, MinGW can be installed alongside Cygwin. By editing the Cygwin path, calls to gcc or g++ can point to the MinGW bin directory.

Follow the links to learn more about the GCC 4.6 release series and the GCC 3.4 release series.

''Problems? For discussion and mention of user- or system-specific issues, please use the "Talk" button above.''

Disclaimer
This how-to is provided without any guarantees whatsoever. This process was tested using the versions of Cygwin and the prerequisite packages listed below.

Warning
After you install GCC >= 4.3, some things like exception handling or shared code may not work (read more here), so install at your own risk. (The new GCC will be installed under /usr/local/bin; you'll still have the old GCC under /usr/bin or /bin).

Verify Cygwin Prerequisites
Check that you have following packages under Cygwin. If something is missing or outdated, download it using the setup.exe utility from the Cygwin site (http://cygwin.com/):
 * bash (found in the Base section)
 * gzip (version 1.2.4 or later; found in the Base section)
 * binutils (found in the Devel section)
 * bzip2 (version 1.0.2 or later; found in the Utils section)
 * gcc-core (or any other C compilier; found in the Devel section)
 * gcc-g++ (needed only if you want to install c++ compilier; found in the Devel section)
 * gcc-ada (needed only if you want to install Ada compilier; any version; found in the Devel section)
 * m4 (found in the Interpreters section)
 * make (version 3.79.1 or later; found in the Devel section)
 * Either/or:
 * zip and unzip (needed only if you want to install Java; found in the Archive section)
 * gcc-java (needed only if you want to install Java; only jar command is needed; found in the Devel section)

You can check package version numbers and statuses with the cygcheck command: cygcheck -c \ bash binutils bzip2 cygwin gcc-core gcc-g++ gcc-java gzip m4 make unzip zip

An example of the 'Cygwin Package Information output: Package             Version        Status bash                4.1.10-4       OK    binutils             2.20.51-2      OK    bzip2                1.0.6-1        OK    cygwin               1.7.9-1        OK    gcc-core             3.4.4-999      OK    gcc-g++              3.4.4-999      OK    gzip                 1.4-1          OK    m4                   1.4.16-1       OK    make                 3.81-2         OK    unzip                6.0-10         OK    zip                  3.0-11         OK

Gather GCC and Its Prerequisites
First, create a workspace directory to build in (e.g., mkdir /usr/local/contrib). This is where you'll extract and work with each GCC-related package, including GCC itself.

Gather the GCC prerequisite packages:
 * Go to http://gmplib.org/ and download the source (e.g., gmp-5.0.2.tar.bz2). GMP is a library for arbitrary precision arithmetic. It is required for MFPR and thus for GCC. The version from Cygwin is older and will not work.
 * Go to http://mpfr.org/ and download the source (e.g., mpfr-3.0.1.tar.bz2). MPFR is a library for arbitrary precision floating-number arithmetic which produces exactly same results for any CPU or operating systems. It is required for GCC.
 * Go to http://www.multiprecision.org/ and download the source (e.g., mpc-0.9.tar.gz). MPC is a library for arbitrary precision complex number arithmetic and is required for GCC.
 * Go to http://gcc.gnu.org/mirrors.html, select a working mirror, browse to the gcc folder and the version numbered subfolder, and download the full GCC tarball for that version (e.g., gcc-4.6.0.tar.bz2) You will see MANY things in a given GCC version's folder... the main archive will contain everything you need.

Check Environment Variables
The environment variables LD_LIBRARY_PATH, LIBRARY_PATH, and CPATH must be set correctly:
 * LD_LIBRARY_PATH and LIBRARY_PATH should contain /usr/local/lib (LIBRARY_PATH shall not be set on Enterprise Linux Enterprise Linux Server release 5.5 (cartage))
 * CPATH should contain /usr/local/include
 * If PATH contains c:/windows/system32 (or /cygdrive/c/windows/system32; case-insensitive), it should be after /bin and /usr/bin. Otherwise the scripts will try to run Windows sort.exe instead of the Unix command with the same name.

Viewing settings (paste all the lines - works in either shell): echo ;\ echo LD_LIBRARY_PATH=${LD_LIBRARY_PATH}; \ echo LIBRARY_PATH=${LIBRARY_PATH}; \ echo CPATH=${CPATH}; \ echo PATH=${PATH}; \ echo

Settings example (bash): export LD_LIBRARY_PATH=/usr/local/lib export LIBRARY_PATH=/usr/local/lib export CPATH=/usr/local/include export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS
 * 1) Depending on system, LIBRARY_PATH shall not be set -
 * 2)   export LIBRARY_PATH=

Settings example (csh/tcsh): setenv LD_LIBRARY_PATH /usr/local/lib setenv LIBRARY_PATH /usr/local/lib setenv CPATH /usr/local/include setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS

These settings should also should be included into your .bash_profile or .profile (if you use bash) or .cshrc / .tcshrc (if you use csh/tcsh).

Pre-Build Notes
The make utility has the "-j n" option for parallel build jobs, where "n" = the maximum number of jobs to spread the build across. Running it as   make -j 2 ... can significantly speed up the build, particularly on multi-core/hyper-threaded processors. Generally it is best to keep n <= the number of real or virtual cores you have (in other words, if you have a quad core and try running -j 20 you may not be doing yourself any favors over -j 8 or possibly even -j 4, and might have other problems such as excessive swap usage that negate any possible speed improvement).

Beware: sometimes multi-job builds can fail or hang (if the build hangs for a few minutes with zero CPU being used by various, unchanging build sub-processes, as seen in Process Explorer or Task Manager, this suggests a possible deadlock).

Also, if you have Spybot Search & Destroy installed, its TeaTimer functionality will slow down the build/install process. You may want to disable it or kill it in Task Manager for the duration of this build/install process.

A Special Note about Logging Your Build
You've spent a few hours building and something goes wrong. Logging your build (or ANY non-interactive command's output) is a quick and painless sanity-saver when you know what to do!

For bash: somecommand -someargs -moreargs -yougettheidea    2>&1 | tee mylogfile.log

For csh/tcsh: somecommand -someargs -moreargs -yougettheidea    |& tee mylogfile.log

Build and Install GCC's Prerequisites
Perform the following build/install steps for the GMP, MPFR and MPC packages in that order : ln -s $ac_rel_source $ac_dest 2>/dev/null || ln $srcdir/$ac_source $ac_dest || You need to comment out the first one so it looks like this. #ln -s $ac_rel_source $ac_dest 2>/dev/null || ln $srcdir/$ac_source $ac_dest || The second change is to mcp, in the mpc-0.9/src folder you need to edit the line of "mpc.h" #if defined(__MPC_WITHIN_MPC) && __GMP_LIBGMP_DLL to  #if defined __GMP_LIBGMP_DLL
 * 1) cd to your workspace directory (above, e.g., cd /usr/local/contrib)
 * 2) Extract the tarball using tar (e.g., tar -xf gmp-5.0.2.tar.bz2). This will create a sub-folder with the source for the given package
 * 3) cd into that source folder (e.g., cd gmp-5.0.2)
 * 4) Read the README and/or INSTALL file if present (but that's why you're here after all...)
 * 5) Run ./configure (this will check the configuration of your system for the purpose of this package)(you also need specify --enable-static --disable-shared when compiling the library)
 * 6) Run make (this will build the package; -j can speed things up here)
 * 7) Run make check (strongly recommended but optional; this will check that everything is correct - special caveat for the GCC package itself, below)
 * 8) Run make install (this will install all the relevant files to the relevant directories)
 * 9) Run make clean (optional; this will erase intermediate files - important if you are re-attempting a broken build!)
 * 10) Note that for the current versions of gmp (5.0.5) and mpc (0.9) two changes need to be made to have the build work successfully. In the gmp configure file there are two lines

Build and Install GCC
Perform the following build/install steps for GCC (note how this differs significantly from the instructions above!):
 * 1) cd to your workspace directory (above, e.g., cd /usr/local/contrib)
 * 2) Extract the tarball using tar (e.g., tar -xjf gcc-4.6.0.tar.bz2). This will create a sub-folder with the source for the given package. This might take significantly longer than the other packages, so be patient.
 * 3) YOU MUST NOT ATTEMPT TO BUILD GCC FROM SOURCE DIR.
 * 4) Instead create a new build folder at same level. Create a build folder (e.g., mkdir build) in your workspace. Thus you will have the gcc-4.6.0 folder and build folder at the same directory level.
 * 5) cd into the build folder (e.g., cd build)
 * 6) You may want to read the README and/or INSTALL file if present
 * 7) Run ../gcc-*/configure --enable-threads=win32 --enable-languages=c,c++ (The "--enable-languages" option specifies here to build gcc (the C compiler) and g++ (the C++ compiler). GCC actually includes many languages. If you leave out this option you'll build all of them, which could easily take 4 hours or so. See the GCC configuration page for details. If you'll need to install multiple versions of GCC, you'll want to use the --prefix argument so they don't all end up in /usr/local. See the configure --help output for details.)
 * 8) Run make (this will build the package; -j can speed things up here - this will take a while even with that!) The comparison step between stage 2 and 3 takes a particularly long time... it'll seem hung but your process/task monitor will show activity. The two specific warnings "gcc/cc1-checksum.o differs" and "gcc/cc1plus-checksum.o differs" are not unusual and should not themselves cause a build failure (the "compare:" target in build/Makefile specifically manages this).
 * 9) Skipping make check: For GCC on Cygwin we CANNOT run make check! (You cannot perform the make check step because it requires autogen which in turn requires libguile which in turn requires libtool. If you would try to install libtool, guile, and autogen, then you will see that guile (v 1.8.4) has some problems (fails 2 tests of 64) and autogen (v 5.9.4) does not work (fails 18 tests of 20)).
 * 10) Run make install (this will install all the relevant files to the relevant directories)
 * 11) Run make clean (optional; this will erase intermediate files - important if you are re-attempting a broken build!)

After GCC is Built and Installed
The new GCC will be installed under /usr/local/bin, while /bin/gcc and /usr/bin/gcc will still point to the original Cygwin GCC. If you want to use new GCC by default, make sure that your path contains /usr/local/bin before /bin and /usr/bin

You may view your path using printenv PATH

Run a quick sanity test: echo; gcc --version; echo; g++ --version;  gcc (GCC) 4.6.0 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ (GCC) 4.6.0 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE EVER.

Last edited
MartyMacGyver 21:36, July 10, 2011 (UTC)