Solarian Programmer

My programming ramblings

Raspberry Pi - Install GCC 6 and compile C++14 and C++17 programs

Posted on June 24, 2016 by Sol

This is a short article about how to get started with C++14 and C++17 on Raspberry Pi on Raspbian. At the time of this writing Raspbian is based on Debian Jessie, which comes with the stable but outdated GCC 4.9 as the default C and C++ compiler.

Fortunately, the next release of Debian, Stretch, comes with GCC 6.1 which has a complete C++11/C++14 implementation. I wouldn’t recommend a complete upgrade from Jessie to Stretch because, at this time, not all packages from Stretch have proper support for Raspberry Pi, this is why I will show you next how to install GCC 6 and his dependencies from Stretch and keep Jessie as the default source for all the other packages. I’ve tested the next steps on Raspberry Pi 2 and 3, but it should work on older models too.


Some people reported problems with apt-get after they’ve used packages from Stretch. Please keep in mind that, at this time, Stretch probably has bugs. So, before using Stretch make sure to backup your data or, even better, use a spare SD card with a fresh Raspbian install.

If you want to be perfectly safe I recommend building GCC 6 from sources. While this procedure could take some time - about 7 hours on a Raspberry Pi 3 your system won’t be affected by any potential Stretch bug.

Now that you’ve been warned, let’s proceed.

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):

1 sudo apt-get update
2 sudo apt-get upgrade

Next, open /etc/apt/sources.list in your favorite editor and repalce jessie with stretch:

1 sudo vim /etc/apt/sources.list

Update your package list:

1 sudo apt-get update

and, finally, install GCC 6:

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

If you need a Fortran compiler, you can install it with:

1 sudo apt-get install gfortran-6

Last step is to revert back from Stretch to Jessie, open /etc/apt/sources.list and replace stretch with jessie, after that do an update to refresh the package list:

1 sudo vim /etc/apt/sources.list
2 sudo apt-get update

The above procedure will keep GCC 4.9 as the default C and C++ compiler for any package that depends on it, if you want to compile a program with GCC 6 you will have to use gcc-6 or g++-6 when invoking the compilers.

In GCC 6 the default C++ standard is now -std=gnu++14, which means that by default any C++ program will be treated as a C++14. For C, the default is std=gnu11 starting from GCC 5.

Let’s try to compile and run a C++14 code that uses a generalized lambda expression:

 1 #include<iostream>
 2 #include<complex>
 4 int main() {
 5     // Store a generalized lambda, that squares a number, in a variable
 6     auto func = [](auto input) { return input * input; };
 8     // Usage examples:
 9     // square of an int
10     std::cout << func(10) << std::endl;
12     // square of a double
13     std::cout << func(2.345) << std::endl;
15     // square of a complex number
16     std::cout << func(std::complex<double>(3, -2)) << std::endl;
18     return 0;
19 }

Assuming that you’ve saved the above code as lambda_test.cpp, you can compile it with:

1 g++-6 -Wall -pedantic lambda_test.cpp -o lambda_test

or, if you want to disable any GNU extensions and use the ISO C++14 standard:

1 g++-6 -std=c++14 -Wall -pedantic lambda_test.cpp -o lambda_test

If you run the code, this is what you should see:

1 pi@raspberrypi ~ $ g++-6 -std=c++14 -Wall -pedantic lambda_test.cpp -o lambda_test
2 pi@raspberrypi ~ $ ./lambda_test
3 100
4 5.49903
5 (5,-12)
6 pi@raspberrypi ~ $

The C++17 standard is not finalized at the time of this writing, however if you want to compile a C++ program in C++17 mode you can use:

1 g++-6 -std=c++1z -Wall -pedantic lambda_test.cpp -o lambda_test

For an overview of C++17 support in GCC see

If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.

or Effective Modern C++ by Scott Meyers.

comments powered by Disqus