http://anadoxin.org/blog

New GCC on an old Linux distribution

Tue, 05 August 2014 :: #linux :: #cpp

Sometimes you need to have a new version of gcc (i.e. to be able to leverage the features from C++11 standard), but you have an old distribution which doesn't have new gcc inside its repository. Here's how you can compile your own gcc on that distribution.

Acquire gcc tarball

First you need to download the main gcc tarball from the official gcc website:

$ wget ftp://gd.tuwien.ac.at/gnu/gcc/releases/gcc-4.9.0/gcc-4.9.0.tar.bz2

After downloading, unpack the archive to some directory. Make sure you'll have enough space to compile the whole package though. I don't remember exactly how much space it will take, but it's safe to assume that it will take a lot.

If you're installing it on a virtual machine (I did), maybe it's a good idea to use some kind of networked file system solution, like NFS, or at last Samba (sshfs should also work). This way you would host all of the temporary object files on you host machine, which probably has lots of gigabytes of free space, ready to be used (right?).

$ tar xfj gcc-*.tar.bz2

This will unpack the package to the current directory. For the purposes of this post, I'll assume this directory will be named /srv/vm.

Acquire prerequisites

Next, you'll need some thirdparty libraries required by gcc.

1. GMP

GMP (GNU Multiple Precision Library) - website. Unpack it with:

$ tar xfj gmp-*.tar.bz2

Then create symbolic link of newly created directory to the gmp directory, like this:

$ ln -sf gmp-6.0.0 gmp

It should result in having the directory placed in /srv/vm/gcc-4.9.0/gmp.

This way the build system of gcc should handle building this library together with gcc itself.

2. MPFR

MPFR (Multiple-Precision Floating-point computations with correct Rounding) - website. Unpack it, and create a symbolic link of its directory to the mpfr directory.

3. MPC

MPC (expansion over MPFR if I understand it correctly) - website. Unpack it, and create a symbolic link of its directory to the mpc directory.

4. ISL

ISL (Integer Set Library) - website. Unpack it, and create a symbolic link of its directory to the isl directory.

If you're getting some compilation errors related to isl_int, use an older version of the library, before 0.13. The 0.13 version removes the isl_int symbol, so this major update could be too major for other libraries (like CLooG). The version that worked for me is 0.12.2.

If you do need to change the version of the ISL library, make sure you'll clean the current configuration cache, created after running configure script. Best way is to remove the whole source directory and recreate it from scratch. Oh, maybe make distclean will help too.

5. CLooG

CLooG (Chunky LOOp Generator) - website. Unpack it, and create a symbolic link of its directory to the cloog directory.

Of course, you will also need standard combo in the form of autotools, older version of the compiler, but that's pretty obvious, so I'll just skip it.

Compilation

After installing the dependencies, next step is pretty straightforward:

$ ./configure --disable-multilib --prefix=/usr/local/gcc49 --enable-threads --enable-languages=c,c++
$ make

It is probably OK to get some errors, as long they're not stopping the build process. Please note that this process will take a long time, so you'll have to plan it ahead.

If you're trying to compile it through an ssh connection, I suggest you use a terminal detaching tool like screen or tmux, so you'll be able to close the connection without interrupting the build.

To finish the process, don't forget to make install. Your new gcc will be installed to /usr/local/gcc49.

Please note that you may need to use -static-libstdc++ and -static-libgcc options when linking your executable file! See the proper documentation of gcc to read more about characteristics of these options.