[Update: see An even simpler recipe for building GCC for ... yeah, an even simpler recipe for building GCC]
A simple recipe for building GCC
Building GCC is not trivial, but is not difficult if you follow the instructions carefully.
Many people rush into trying to build it without reading the installation docs properly and make one or more of these common mistakes:
1) do not run ./configure - this is not supported, you need to run configure from outside the source directory
2) if GCC links dynamically to the prerequisite libs (GMP/MPFR/MPC) then the shared libraries must be in the dynamic linker's path, both when building gcc and when using the installed compiler.
These problems are easily avoided by reading http://gcc.gnu.org/install/prerequisites.html, http://gcc.gnu.org/install/configure.html, http://gcc.gnu.org/wiki/FAQ#configure and http://gcc.gnu.org/wiki/FAQ#configure_suffix but noone does that.
For the impatient or RTFM-intolerant, a foolproof recipe for building GCC is given below.
The trick to this recipe is that the GMP, MPFR and MPC prerequisites are not installed separately, they are built as part of gcc and linked to statically. This avoids the common problem of installing the shared libraries in a non-standard location and having to tell the dynamic linker how to find them. This method is documented at http://gcc.gnu.org/install/prerequisites.html and is much easier than building and installing the prerequisites separately, but everyone seems to choose the hard way.
THIS RECIPE IS NOT A SUBSTITUTE FOR RTFM.
If you decide to stray from this recipe without reading the docs do not be surprised if you get indigestion.
* Ingredients:
1 gcc source package (e.g. gcc-4.6.0.tar.gz)
Alternatively, download individual packages for each GCC language front end (e.g. gcc-core, gcc-g++ etc.)
1 gmp source package (e.g. gmp-5.0.1.tar.gz)
1 mpfr source package (e.g. mpfr-3.0.1.tar.gz)
1 mpc source package (e.g. mpc-0.8.1.tar.gz)
* Method:
First prepare your environment, season these variables to taste:
# the versions you will build
gccver=4.6.0
gmpver=5.0.1
mpfrver=3.0.1
mpcver=0.8.1
# where you put the downloaded source packages
pkgdir=$HOME
# where you will build gcc
rootdir=$HOME/gcc-tmp
# where you want to install gcc
prefix=/opt/gcc-${gccver}
# the languages you want gcc to support
langs=c,c++
Create a new directory on a disk with plenty of space and unpack the sources there:
mkdir ${rootdir}
cd ${rootdir}
tar xzf ${pkgdir}/gcc-${gccver}.tar.gz
tar xzf ${pkgdir}/gmp-${gmpver}.tar.gz
tar xzf ${pkgdir}/mpfr-${mpfrver}.tar.gz
tar xzf ${pkgdir}/mpc-${mpcver}.tar.gz
Next, move the prerequisite sources into the gcc source directory:
mv gmp-${gmpver} gcc-${gccver}/gmp
mv mpfr-${mpfrver} gcc-${gccver}/mpfr
mv mpc-${mpcver} gcc-${gccver}/mpc
Now create a build directory and change to it
mkdir objdir
cd objdir
Now configure gcc:
${rootdir}/gcc-${gccver}/configure --prefix=${prefix}
--enable-languages=${langs}
Now build gcc:
make
Finally, install gcc:
make install
Your compiler is now ready to use.
If something goes wrong you can just remove the entire $rootdir/objdir directory and recreate it and run configure again. The source dir will be unchanged because it is never altered when you build in a separate dir.