How to build/install gcc/g++ from sources on a Raspberry Pi

Print Friendly, PDF & Email

The easiest, fastest and painless solution is using a pre-compiled package: some apt-get install stuff after added the proper repo of the distro generally does the trick.

But fortunately GCC/G++ are always in progress tools and it is useful to be able to align to the most recent version autonomously.

So, this the base directory of the GCC ftp repo:

https://ftp.gnu.org/gnu/gcc/

choose the subdir of the version you want and download it with wget:

wget https://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.gz

untar/unzip it and move to the directory:

tar xzvf gcc-6.3.0.tar.gz

some prerequisites must be downloaded as well:

./contrib/download_prerequisites

then we’ll go back to our home dir and we’ll create a directory that we’ll use for building the sources; starting from this dir we configure the MakeFile:

cd ~
mkdir gcc-build
cd gcc-build
../gcc-6.3.0/configure -v --prefix=$HOME/gcc-6.3.0

Just a note about the --prefix=$HOME/gcc-6.3.0 switch; the gcc online documentation states:

We use srcdir to refer to the toplevel source directory for GCC; we use objdir to refer to the toplevel build/object directory.
[…]
--prefix=dirname
Specify the toplevel installation directory. This is the recommended way to install the tools into a directory other than the default. The toplevel installation directory defaults to /usr/local.

We highly recommend against dirname being the same or a subdirectory of objdir or vice versa. If specifying a directory beneath a user’s home directory tree, some shells will not expand dirname correctly if it contains the ‘~’ metacharacter; use $HOME instead.

so, depending where you are working, you could change $HOME/gcc-6.3.0 with the directory where you put the sources.

For an ARM architecture some other switches must be used – my config command line (for a R-PI 3) was like this:

../gcc-6.3.0/configure -v --enable-languages=c,c++ --prefix=/media/usb/gcc/gcc-6.3.0 --program-suffix=-6.3.0 --with-arch=armv6 --with-fpu=vfp --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf

At this point just do the make jump – this will compile gcc/g++ and depending on your machine the time needed may vary; a RaspberryPi 3 will need a lot of time (many, many hours):

make

when it’s done, we can install it in the system:

sudo make install

Disk space needed

The whole thing is going to take quite a lot of disk space: to host sources and build version 6.3.0 I had to use 4.6GB of disk space.

Leave a Reply

Your email address will not be published. Required fields are marked *