Install GCC

From Micro and Nano Mechanics Group
Jump to navigation Jump to search

GCC stands for GNU Compiler Collection. The latest version is GCC 4.3. The use of this tutorial is to install GCC 4, which is not available in some linux distributions. In those systems GCC 3 is available instead but I suppose that for some reason you need GCC 4. (Of course we will use GCC 3, or any other available C compiler to build GCC 4.) The tutorial is focused in obtaining 'g++' (GNU C++) among all the available compilers in the collection. This tutorial is based on this other guide.

You can check which version of GCC is currently installed by running

 $gcc -v
 ...
 gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)

Preparation

The tutorial assumes that you want to install GCC/g++ in your userspace directories (i.e. in your home directory). To do that create the following directories:

 mkdir ~/soft
 mkdir ~/usr

You will need the GMP library whose development (sources) are not installed in general (and in particular not in wcr).

 cd ~/soft
 wget ftp://ftp.gnu.org/gnu/gmp/gmp-4.2.4.tar.gz
 tar -zxvf gmp-4.2.4.tar.gz
 cd gmp-4.2.4
 ./configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local
 make
 make check
 make install

Which will create ~/usr/lib/libgmp.[a,la,so] and ~/usr/include/gmp.h. Also you will need the MPFR library after GMP.

 cd ~/soft
 wget http://www.mpfr.org/mpfr-current/mpfr-2.4.1.tar.gz
 tar -zxvf mpfr-2.4.1.tar.gz
 cd mpfr-2.4.1
 ./configure --prefix=$HOME/usr --with-gmp=$HOME/usr
 make
 make check
 make install

This will create ~/usr/lib/libmpfr.[a,la,so] and ~/usr/include/mpfr.h.

Now, Download the sources to a local directory:

 cd ~/soft
 wget http://gcc.releasenotes.org/releases/gcc-4.3.3/gcc-g++-4.3.3.tar.gz
 tar -zxvf gcc-g++-4.3.3.tar.gz

Compilation

This is important and different from other usual compilation procedures: GCC should be compiled in a directory different from the source directory, in this case we will create a "build" directory.

 cd ~/soft
 mkdir gcc-4.3.3-build
 cd gcc-4.3.3-build

Then we will 'configure' from that directory. Everything will be installed in ~/usr, including the executable compilers in ~/usr/bin and the library files in ~/usr/lib. To specify that you do

 cd ~/usr/gcc-4.3.3-build
 ../gcc-4.3.3/configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local --with-gmp=$HOME/usr --with-mpfr=$HOME/usr --enable-languages=c++

Other options could be specified at this point, for example, GCC 3 in wcr was configured with this options

 ... --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux

Which may or may not be important for you. Check that the configure works (if it doesn't, report it in this wiki) before doing:

 export LD_LIBRARY_PATH=$HOME/usr/lib
 make #takes ~ minutes

You can then 'test' many of the bundled compilers, in particular g++:

 make test-g++

If the test was succesful, we do the final install:

 make install

Important: there is no 'uninstall', if you want to remove GCC from ~/usr you have to do it manually which can be very painful.