User Tools

Site Tools


This is an old revision of the document!

Compiling GCC from Source

This guide is written as to try and help CHPC users who are struggling to compile the GCC version required for their codes. For the official guide on compiling GCC please visit the GCC website. This guide documents how gcc-4.6.2 would be built on the Sun Tsessbe Cluster login node.

Before you begin, make sure your bash environmental variable are set correctly, for example . Also it recommended that you make you use of temporary or scratch directory to do all the compilation in (i.e. ~/scratch or /tmp/$USER), as to save disk space in your home directory.

Also note that for this guide, all binary and libraries are installed home directory, i.e prefix=$HOME. My setup

$ export PATH=/opt/gridware/gcc-4.3/bin:$PATH
$ export LD_LIBRARY_PATH=/opt/gridware/gcc-4.3/lib/:$LD_LIBRARY_PATH
$ export LD_LIBRARY_PATH=/opt/gridware/gcc-4.3/lib64/:$LD_LIBRARY_PATH
$ gcc --version
  gcc (GCC) 4.3.4


A table showing what tools and package for building GCC, and what is available on the cluster login node (this is where GCC will be built)

Prerequisite Version Required (or later) Available
ISO C90 compiler GCC binary version 2.95 gcc 4.3.4
GNAT ? ?
A “working” POSIX compatible shell, or GNU bash bash available
awk GNU awk version 3.1.5 GNU awk version 3.1.5
GNU binutils ?
gzip 1.2.4 1.3.5
gzip2 1.0.2 1.0.3
GNU tar 1.14 1.15.1
Perl 5.6.1 5.8.8
jar, or InfoZIP ?
GMP Library 4.3.2 4.1.3
MPFR Library 2.4.2 No
MPC Library 0.8.1 No

So the GMP, MPFR and MPC support libraries need to build before install gcc. Also note from “While any sufficiently new version of required tools usually work, library requirements are generally stricter. Newer versions may work in some cases, but it's safer to use the exact versions documented”


Download the GMP source code

$ cd ~/scratch #or cd /tmp/$USER if luster is misbehaving ..
$ wget  
$ tar -xf gmp-4.2.4.tar.bz2

Configure GMP

$ cd gmp-4.2.4
$ mkdir BUILD
$ cd BUILD
$ ../configure --prefix=$HOME

Build, if this process is interrupted try run make again

$ make

Test it!

$ make check

If all the test are passed (no critical error is thrown for `make check'), install GMP

$  make install


Download the MPFR source code

$ cd ~/scratch #or cd /tmp/$USER if luster is misbehaving ..
$ wget
$ tar -xf mpfr-2.4.2.tar.gz

Configure MPFR

$ cd mpfr-2.4.2
$ mkdir BUILD
$ cd BUILD
$ ../configure --with-gmp=$HOME --prefix=$HOME

NB make sure you see `checking if gmp.h version and libgmp version are the same… (4.2.4/4.2.4) yes'. If you do not, its possible your GMP installation failed.

Build it,

$ make


$ make check

If all the test are passed, install

$  make install


Download the MPC source code

$ cd ~/scratch #or cd /tmp/$USER if luster is misbehaving ..
$ wget
$ tar -xf mpc-0.8.1.tar.gz 

Configure GMP

$ cd mpc-0.8.1/
$ mkdir BUILD
$ cd BUILD
$ ../configure --with-gmp=$HOME --with-mpfr=$HOME --prefix=$HOME

Build and test

$ make
$ make check

If all the tests are passed

$ make install

Download GCC

Now that the Prerequisites are ready, GCC can be downloaded (i use an http mirror as the ftp mirrors seem to be blocked?). Download GCC

$ cd ~/scratch #or cd /tmp/$USER
$ wget
$ tar -xf gcc-4.6.2.tar.bz2


$ cd gcc-4.6.2/
$ mkdir BUILD
$ cd BUILD
$ ../configure --with-gmp=$HOME --with-mpfr=$HOME --with-mpc=$HOME --prefix=$HOME

Compiling GCC

Once the configuration is complete, use Make to build GCC


The compilation process may be unstable, throwing errors such as

  • xgcc: internal compiler error: Killed (program cc1)
  • gcj: internal compiler error: Killed (program jc1)

In these cases, the make process was interrupted due to compile command being killed (try killall cc1 from another shell, while make is running to replicate this error…). My guess is that, in order to prevent users from running jobs from the login node, a ulimit is set as to kill processes running for a long time. These errors can be overcome by rerunning make. You can either do this manually, or automatically. The automatic approach entails the following (from inside the build directory):

$ screen
$ for i in `seq 1 25` ; do echo MAKE CALL $i ; make ; sleep 30 ; done >> make.out 2>&1 ; echo MAKE CALLS FINISHED >> make.out
# detach screen using ctrl-D
$ tail -f make.out

The benefit about this screen approach beyond saving you grey hairs, is that if ssh connection is lost or you close terminal, the compilation job will keep going. The compilation can take multiple hours… Beware though, the screen session uses your default environmental variables defined in your .bashrc file.

Testing GCC build

Before testing the GCC build, dejaGNU needs to be installed

$ cd ~/scratch
$ wget
$ tar -xf dejagnu-1.5.tar.gz
$ cd dejagnu-1.5/
$ mkdir BUILD #do not build from src directory!
$ cd BUILD 
$ ../configure --prefix=$HOME
$ make
$ make check # ensure that all test are passed before installation
$ make install

Then sit back for couple of hours while the testing is done, again i used screen so the test wont be interrupted in the event of a lost connection

$ cd ~/scratch/gcc-4.6.2/BUILD
$ screen
$ make -k check > make.check 2>&1
# detach screen using Ctrl-D
$ tail -f make.check

After a couple of hours the tests where complete. Selected summaries from the test results (for the build done for this guide) where as follows

              === gcc Summary ===

# of expected passes            77291
# of unexpected failures        19
# of unexpected successes       24
# of expected failures          229
# of unsupported tests          2067

              === g++ Summary ===

# of expected passes            27637
# of unexpected failures        8
# of unexpected successes       1
# of expected failures          167
# of unsupported tests          371

              === gfortran Summary ===

# of expected passes            38951
# of expected failures          41
# of unsupported tests          67

Which is good enough for my purposes since i installed GCC 4.6.2 for the latest gfortran version which passed all its tests …


If the test results are satisfactory, install GCC to home directory by running

$ make install

from the gcc BUILD directory. You will also want to add the following lines to ~/.bashrc file

export PATH=~/bin:$PATH
export LDFLAGS="-L$HOME/lib64 -L$HOME/lib"
export CPPFLAGS="-I$HOME/include -L$HOME/lib64 -L$HOME/lib"
export F77=gfortran

Next time you login, you should see

$ gcc --version
gcc (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

have fun!

/var/www/wiki/data/attic/guide/compiling_gcc.1334239192.txt.gz · Last modified: 2012/04/12 15:59 by hamish