Building new g++

Jurjen Bokma

Last modified: "2016-12-21 11:25:04 jurjen"


Quick overview of how to build GNU g++ from source on Debian (and derived systems).

Table of Contents
  1. Take a look at the releases overview, and pick the newest version.


    The document you're reading may show a considerably older version. It isn't updated every time GCC is released.

  2. To build GCC from source, additional software is required, like compilers, linkers, libraries and headers. You can read the docs to find out what exactly you need to install.

    On a reasonably well equipped Debian, the following usually goes a long way. First install the build dependencies of the current GCC that the system has available:

    apprentice@host:~/build-gcc-5.2.0$ sudo apt-get build-dep gcc g++

    Then install some optional packages that aren't strictly necessary for building, but are in our practice required:

    apprentice@host:~$ sudo apt-get install libgmp-dev libmpc-dev libmpfr-dev libc6-dev-i386

  3. Fetch the source archive, unpack it, and delete the packed archive if you 're low on disk space.

    apprentice@host:~$ wget
    apprentice@host:~$ tar jxf gcc-5.2.0.tar.bz2
    apprentice@host:~$ rm gcc-5.2.0.tar.bz2

  4. Gcc needs to be built in a separate build directory, which shouldn't be a subdirectory of the source.

    apprentice@host:~$ mkdir build-gcc-5.2.0
    apprentice@host:~$ cd build-gcc-5.2.0

  5. We want to new compiler in /usr/local, where it doesn't interfere with the package system:

    apprentice@host:~/build-gcc-5.2.0$ ../gcc-5.2.0/configure --prefix=/usr/local --enable-languages=c,c++
    checking whether to enable maintainer-specific portions of Makefiles... no
    configure: creating ./config.status
    config.status: creating Makefile

    If you are still missing other software that is needed to start building, this command will finish with an error. In that case, read the documentation and fix the problem.

    If you omit the --enable-languages option, more compilers will be built. You could also face a build time of twice as long as for C and C++, or more.


    For more optimization features, you may want to

    ../gcc-6.1.0/configure --prefix=/usr/local --enable-languages=c,c++ --enable-libquadmath --enable-lto --with-isl=/usr/local

    In that case, you have to fetch, build and install isl as well, which can be retrieved from the GNU infrastructure ftp server. Also, don't forget to run ldconfig.

  6. The -j2 is to tell the compiler to build in two parallel threads. Find out how many CPUs your computer has (cat /proc/cpuinfo |grep ^processor), and start at least that number of threads. It greatly speeds up building.

    apprentice@host:~/build-gcc-5.2.0$ make -j2 bootstrap

    But have some coffee anyway. The test PC took nearly 1.5 hours to build just gcc and g++.

  7. Now that the building has finished without errors, the resulting programs, libraries and headers can be installed:

    apprentice@host:~/build-gcc-5.2.0$ sudo make install

  8. On a significantly older Debian, you cannot just install a brand new compiler and expect things to Just Work. When you compile with it, you also need to tell it where to look for some of its own new libraries and header files. The following shell snippet may come in handy:

    # This is a bash snippet, to be sourced from .profile
    # NB Do not source this snippet multiple times. It'll clutter your environment.
    # Find all dirs containing libraries
    LIBDIRS="$(find ${GCCBASE} -iname \*\.so\.\* -exec dirname {} \;|sort |uniq|grep -v 32$|tr '\n' ':')"
    # If you want the newer tools by default, put them at the beginning of $PATH
    export PATH="${PATH}:${GCCBASE}/bin"
    # Have Make pick the newer compiler
    export CXX="${GCCBASE}/bin/g++"
    export CC="${GCCBASE}/bin/gcc"
    # For good measure: default compiler flags for the C++ course
    export CXXFLAGS="${CXXFLAGS} -Wall -std=c++14"
    # This is for during compile: prefer newer headers
    # This is for linking during build: prefer newer libraries
    # (with 5.2, this seems to affect run-time)
    # WIth 5.2, this affects build time
    # This is for runtime linking: prefer older libraries
    #export LD_RUN_PATH="${LD_RUNPATH}:${GCCBASE}/lib/../lib64"
    # Man should also find the newer manpages
    export MANPATH="${MANPATH}:${GCCBASE}/share/man"
    echo "Set up to use ${CXX} and ${CC}"

    Assuming you have the world famous Hello World code in a file, you can compile it like this:

    apprentice@host:~$ ${CXX} -o hw

    Make-like programs heed ${CXX}, so programs using such utilities Just Work automatically:

    apprentice@host:~$ make hw
    apprentice@host:~$ ./hw
    Hello, world!


    Programs made with this newer compiler and newer libraries also use a newer ABI than the regular compiler/libraries on the LWP. If you run them without the environment settings above, you'll get relocation errors from the linker. This can be prevented by hardcoding the library paths during building, and that is done with such options as:

    export LDFLAGS='-Wl,--rpath,/opt/netapps/gnu/gcc/5.2/lib64 -Wl,--rpath,/opt/netapps/gnu/gcc/5.2/libexec/gcc/x86_64-unknown-linux-gnu/5.2.0 -Wl,--rpath,/opt/netapps/gnu/gcc/5.2/lib/gcc/x86_64-unknown-linux-gnu/5.2.0 -Wl,--rpath,/opt/netapps/gnu/gcc/5.2/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/plugin -Wl,--rpath,/opt/netapps/gnu/gcc/5.2/lib/gcc/x86_64-unknown-linux-gnu/lib64'

    The weird dual-comma syntax means to tell the compiler (-Wl) to tell the linker to use options --rpath /opt/netapps/gnu/gcc/5.2/lib64 etc.

    While this use of rpath is hard to avoid, it is in general a bad idea