Building gcc 4.8 from source on Ubunu 12.04

January 16th, 2014 at 6:08 am

The default gcc (4.6.x) on Ubuntu 12.04 is quite old, especially given the quick advance in C++11 capabilities in gcc 4.7 and 4.8 (and, importantly, their respective libstdc++ libraries).

The LLVM project has recently decided (and implemented, earlier this week) to set gcc 4.7 and Clang 3.1 as the minimal versions LLVM & Clang themselves would build with, in order to be able to use C++11 capabilities in the implementation. Therefore, if you want to build trunk LLVM & Clang on Ubuntu 12.04, you need a newer gcc (even if you use Clang to self-build, still libstdc++ version 4.7 or later is required).

Luckily, building a new gcc and installing it locally (to not mess with the system installation) is fairly easy. Here’s a short sequences of steps.

First, create a place to hold the installation, like $HOME/install/gcc-4.8.2.

Install some dependencies needed to build gcc:

$ sudo apt-get install libmpfr-dev libgmp3-dev libmpc-dev flex bison

Then, get a gcc 4.8 tarball and unpack it:

$ wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
$ bunzip2  gcc-4.8.2.tar.bz2
$ tar xvf gcc-4.8.2.tar

Enter the untarred gcc directory:

$ cd gcc-4.8.2/
$ mkdir build
$ cd build

Now, configure and build:

$ ../configure --disable-checking --enable-languages=c,c++ \
  --enable-multiarch --enable-shared --enable-threads=posix \
  --program-suffix=4.8 --with-gmp=/usr/local/lib --with-mpc=/usr/lib \
  --with-mpfr=/usr/lib --without-included-gettext --with-system-zlib \
  --with-tune=generic \
  --prefix=$HOME/install/gcc-4.8.2
$ make -j8
$ make install

This places an installation of gcc 4.8.2 in $HOME/install/gcc-4.8.2. For example, you should be able to see this:

$ cd $HOME/install/gcc-4.8.2
$ find . -name gcc4.8
./bin/gcc4.8

That’s it. You can try it:

$ cd /tmp
$ cat > test.c
int main() { return 42; }
$ $HOME/install/gcc-4.8.2/bin/gcc4.8 test.c
$ ./a.out; echo $?
42

You can also run gcc4.8 with the -### flag to see the exact compilation steps and notice which libraries get picked up, etc. (note that to see -lstdc++ a C++ source compiled with g++4.8 is needed instead).

As a bonus, trunk LLVM & Clang can be now build by passing this gcc to the configure script (the exact same idea works with the CMake-based configuration flow too):

$ CC=$HOME/install/gcc-4.8.2/bin/gcc4.8 \
  CXX=$HOME/install/gcc-4.8.2/bin/g++4.8 \
  ../../llvm-src/configure

Related posts:

  1. Building and using llvmpy – a basic example
  2. Basic source-to-source transformation with Clang
  3. Announcing pss: a tool for searching inside source code
  4. Installing Python 2.7 on Ubuntu
  5. Installing Python 2.5 on Bluehost

7 Responses to “Building gcc 4.8 from source on Ubunu 12.04”

  1. Michael SartainNo Gravatar Says:

    I tried building in a clean 64-bit 12.04 chroot, and had to install the zlib1g-dev and libc6-dev-i386 packages in addition to the ones you listed above. Also had to set these:

    export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
    export CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
    export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu

    Curious as to why I needed those and you didn’t. Are you building in 32-bit 12.04, or is there a better way to handle that?

    Thanks for this post Eli. Very useful. Helped me get symbols in gcc4.8 which is assisting in tracking down the a 20+ minute compile in one of our source files.

  2. elibenNo Gravatar Says:

    @Michael,

    Thanks! As for zlib – I must have had it installed already. Since I wasn’t installing it on a fresh machine, I didn’t stumble on missing dependencies that were already there.

    As for setting these env vars, that’s strange. I don’t have them, and didn’t have to set them. My build was also on a 64-bit 12.04

  3. Daniel LandauNo Gravatar Says:

    You can use “apt-get build-dep gcc” to install all missing build dependencies.

  4. Michael SartainNo Gravatar Says:

    Not to derail the original topic too much, but there’s a good chance I’ll learn more here so I’m going for it… I’ve found that build-dep oftentimes wants to install things I really don’t need. For example, with valgrind I do this: sudo apt-get build-dep valgrind and it gives me a big list of packages dealing with 32-bit and x32: libc6-i386, libc6-dev-i386, libc6-x32, etc.

    I only want to build the 64-bit valgrind though. Is there a way to specify a specific platform with build-dep I haven’t found or do folks just go for it? Thanks!

  5. Vivek KumarNo Gravatar Says:

    First of all Lots of thanks for this post, as first time in my life I’m installing gcc from source.

    Can you please extends this post for showing how to have two version of gcc on the same machine.

    As in your example code, we are using the gcc command by going at the path. I’ll we nice of you, if you explain how to be sure that gcc is taking includes from the pointed directory not from the standard /usr/includes/ *

    Beside this, I’m also finding your other post very nice, clear, intuitive and interesting.

    Thanks a lot for this great stuff

  6. elibenNo Gravatar Says:

    @Vivek,

    It should just work. You can run gcc with the -### flag to observe where it takes headers from.

  7. NimrodNo Gravatar Says:

    Unfortunately, to build a recent gcc you need a good working g++ compiler.

    It is no longer possible to bootstrap gcc using only the system’s C compiler. In fact, it seems like some versions of g++ cannot compile 4.8…

    Given that clang is also c++ based — it becomes much more difficult to bootstrap a modern C++ compiler on a system which does not have one already.

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)