Building GCC from source

Sat, 25 Oct 2014 00:00:00 GMT

I spent a lot of time trying to get my GCC build to work the first time. These are my notes.

Get the source:

$ git clone git://gcc.gnu.org/git/gcc.git
$ cd gcc

To use a specific version, check out one of the tags. For example:

$ git checkout gcc-4_9_0-release

One common problem when building GCC from source is not having the right versions of the GMP, MPFR and MPC libraries installed (or not being able to convince the build system that you do). The trick is to use this magic script which downloads the required library sources into the source tree, where they will be built together with the rest of it: (Also see the Prerequisites for GCC page.)

$ contrib/download_prerequisites

(On my machine I didn't have flex and bison installed, so note to self: # apt-get install flex bison.)

Now the fun part: configure and build. Configuration options are documented here.

$ mkdir build
$ cd build
$ ../configure --prefix=/path/to/gccprefix --enable-languages=c,c++ --enable-checking=release
$ make -j32
$ make install

After plenty of build output, there should now be a freshly baked gcc available in your selected prefix directory:

$ echo 'int main() { puts("hello, world!"); }' | /path/to/gccprefix/bin/gcc -xc - && ./a.out