Raspberry Pi Raspbian - Compiling GCC 8.1
Posted on December 7, 2017 by Paul
Updated 5 May 2018
This is a short article about compiling, building, GCC 8 from sources and how to get started with C++14 and C++17 on Raspberry Pi with Raspbian. At this time Raspbian comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler.
I’ve tested the next steps on a Raspberry Pi 3, but it should work on all current models. Fair warning, compiling GCC from source is a fairly long and intensive process and Raspberry Pi 3 tends to overheat, make sure that you have heat sinks installed or proceed at your own risk. Alternatively, you can use the binary I’ve made, you can can find it on Bitbucket.
I assume that you have a functional Raspbian installation on your Raspberry Pi. Open a Terminal and start by upgrading all your packages from the default Raspbian repository (depending on the speed of your Internet connection this could take some time):
Building GCC 8 from sources could take some time, in my case it took about 5 hours on a Raspberry Pi 3 with a 16GB class 10 SD card and 1GB swap. If you want to avoid the wait time or if you don’t have enough available space, you can download my version from https://bitbucket.org/sol_prog/raspberry-pi-gcc-binary.
Next, let’s download the GCC 8 source and the required prerequisites. Check one of the GCC mirror sites and pick the one closest to you. In my case, I’ve used:
At this point, we can configure the build. In order to keep the system clean, we will use /usr/local/gcc-8.1.0 as the destination for the installed GCC 8. Because we want to be able to use GCC 8 while keeping GCC 6.3, we will append -8.1.0 to the resulting executables.
The above will enable the C and C++ compilers, if you need other compilers like Fortran, for example, add fortran to the configure command:
Now, we are ready to start the build process, as I’ve mentioned earlier this could take a few hours and will use all resources of your RPi. My advice is to restart your Pi in text mode in order to have more RAM disponible for the build process. If you didn’t increased the default swap size for your Raspberry Pi, you can do it by editing the /etc/dphys-swapfile configuration file. Be sure that CONF_SWAPSIZE is at least 1024:
In order to enable the above execute:
Let’s build GCC now:
The above command will try to run 4 jobs in parallel, speeding up the build. If you have any error you can try to lower the number of parallel jobs, use make -j 2 for example.
Once the build process is finished you can install the compilers with:
Add the new compilers to your path with:
At this point, you should be able to compile any C11 or C++17 code using gcc-8.1.0 or g++-8.1.0. If you want to permanently add GCC 8 to your path simply append the above export line to the end of your .bashrc file:
In GCC 8 the default C++ standard is now -std=gnu++14, which means that by default any C++ program will be compiled for C++14. For C, the default is std=gnu11 starting from GCC 5. If you want to use the new C++17 standard use:
Let’s try to compile and run a C++14 code that uses a generalized lambda expression:
Assuming that you’ve saved the above code as lambda_test.cpp, you can compile it with:
If you run the code, this is what you should see:
If the above worked with no error, you can do some clean up in order to save space on your Pi:
If, at some point in the future, you’ll want to get rid of GCC 8 from your system, all you have to do is to remove the gcc-8.1.0 folder from /usr/local, example:
For an overview of C++17 support in GCC see https://gcc.gnu.org/projects/cxx-status.html.
If you are interested to learn more about modern C++, I recommend A Tour of C++ by Bjarne Stroustroup:
or Effective Modern C++ by Scott Meyers.