Last modified: "2016-12-21 11:25:04 jurjen"
Quick overview of how to build GNU g++ from source on Debian (and derived systems).
Take a look at the releases overview, and pick the newest version.
![]() | Note |
---|---|
The document you're reading may show a considerably older version. It isn't updated every time GCC is released. |
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
Fetch the source archive, unpack it, and delete the packed archive if you 're low on disk space.
apprentice@host:~$ wget ftp://ftp.nluug.nl/mirror/languages/gcc/releases/gcc-5.2.0/gcc-5.2.0.tar.bz2
apprentice@host:~$ tar jxf gcc-5.2.0.tar.bz2
apprentice@host:~$ rm gcc-5.2.0.tar.bz2
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
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++
<snip>
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.
![]() | Note |
---|---|
For more optimization features, you may want to
In that case, you have to fetch, build and install |
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++
.
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
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. GCCBASE="/usr/local" # 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 export CPLUS_INCLUDE_PATH="${GCCBASE}/include:${CPLUS_INCLUDE_PATH}" # This is for linking during build: prefer newer libraries # (with 5.2, this seems to affect run-time) export LD_LIBRARY_PATH="${LIBDIRS}:${LD_LIBRARY_PATH}" # WIth 5.2, this affects build time export LIBRARY_PATH="${LIBDIRS}:${LIBRARY_PATH}" # 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 hw.cc
, you can compile it like this:
apprentice@host:~$ ${CXX} -o hw hw.cc
Make-like programs heed ${CXX}, so programs using such utilities Just Work automatically:
apprentice@host:~$ make hw
apprentice@host:~$ ./hw
Hello, world!
![]() | Note |
---|---|
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:
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 |