Setting up GCC 6 on Raspberry Pi3

As of the latest version, MAME now requires GCC 5 as a minimum to compile. Raspbian Jessie ships with GCC 4.9.2 which, at nearly 2 years old, is now just a little too stable. GCC 6.2 is considered stable so there seems no reason to not upgrade. As long as we leave 4.9.2 as the default option, we should be pretty safe.

There are two ways to get GCC 6 the fast easy way with a little risk, or the slow and safe way. Which one you choose is up to you. This is like one of those ‘choose your own adventure’ books isn’t it? 🙂

The easiest way to get GCC 6 is to download a prebuilt package from the Raspbian ‘stretch’ repository. It’s pre compiled for raspberry pi, works, and includes all the required updated dependencies. It’s the version we will all be getting when Raspbian next gets an update. The risk of doing this is that it might cause problems with apt-get in the future, pulling in all these packages that don’t exist in jessie yet.

The safest way is to download and compile it all from source. This will take a long time (12 hours on a Pi2, 6 on a Pi3).

The Easy Way

If you want to just grab the prebuilt one from the stretch repo, this is what you do.

sudo apt-get update
sudo apt-get upgrade
sudo nano /etc/apt/sources.list

Change where it says jessie to say stretch, like this “deb stretch main contrib non-free rpi“, then save it. Now we need to update our sources and grab GCC 6 by typing

sudo apt-get update
sudo apt-get install gcc-6 g++-6

When it has finished, before you do anything else sudo nano /etc/apt/sources.list and change that line back to jessie, then sudo apt-get update again to change the available packages back.

That’s it, all done. GCC 4.9.2 is still the default but you can now use gcc-6 or g++-6 to compile with the newer version.

The Safe Way

To compile GCC6 from scratch, this is what you need to do. Firstly you will be needing a swap space of at least 1024mb, the default is only 100mb because using swap on an SD card will reduce the life of the card dramatically. I use an old external 16gb USB stick for my swap space just because I don’t want to speed the death of my SD card. You can change the way yours is created by editing /etc/dphys-swapfile so lets do that.

sudo nano /etc/dphys-swapfile

Find the line that says CONF_SWAPSIZE=100 and change the value so that it reads
CONF_SWAPSIZE=1024. Save and reboot.

OK we’re back, let’s get on with the build.

mkdir code
cd code
tar xvf gcc-6.2.0.tar.bz2
cd gcc-6.2.0
mkdir obj
cd obj

Then we have some more of that ‘choose your own adventure’ stuff.  For Raspberry Pi B, B+, and Zero, this is the configure line you need…

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

For Raspberry Pi 2, you want this one…

../configure -v --enable-languages=c,c++ --prefix=/usr/local/gcc-6.2 --program-suffix=-6 --with-arch=armv7-a --with-fpu=vfp --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf 
make -j5

…and finally for Raspberry Pi 3, you want to use this.

../configure -v --enable-languages=c,c++ --prefix=/usr/local/gcc-6.2 --program-suffix=-6 --with-cpu=cortex-a53 --with-fpu=neon-fp-armv8 --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
make -j5

Now we just have to wait for 6-24 hours and hope nothing crashes. If everything went according to plan you can now install by typing

sudo make install

and add the the new path to your $PATH by typing

export PATH=/usr/local/gcc-6.2/bin:$PATH

To make that permanent, edit your .profile (if you use bash) or .zshrc (if you use zsh) and add it to the end of that. That’s it, you can test to make sure the right version is being picked up by typing gcc-6 -v and you should see something like this…

Using built-in specs.
Target: arm-linux-gnueabihf
Configured with: ../configure -v --enable-languages=c,c++ --program-suffix=-6 --with-cpu=cortex-a53 --with-fpu=neon-fp-armv8 --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 6.2.0 (GCC) 

Don’t forget to set your swapfile back to 100mb by  editing /etc/dphys-swapfile .

sudo nano /etc/dphys-swapfile

Find the line that says CONF_SWAPSIZE=1024 and change the value so that it reads
CONF_SWAPSIZE=100. Save and reboot.