Solarian Programmer

My programming ramblings

Raspberry Pi - Install GCC 8 and compile C++17 programs

Posted on December 8, 2017 by Sol

Updated 5 May 2018

In this article I will show you how to install GCC 8 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Stretch, which comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler.

If also you want to install Clang 6 on your Raspberry Pi, check my article.

If you want to compile GCC 8 from sources check my previous article.

Let’s start the installation process. Open a Terminal and download a binary of GCC 8:

1 git clone https://bitbucket.org/sol_prog/raspberry-pi-gcc-binary.git

Next, extract the archive and move the extracted compilers to /usr/local:

1 cd raspberry-pi-gcc-binary
2 tar xf gcc-8.1.0.tar.bz2
3 sudo mv gcc-8.1.0 /usr/local

At this point, all you need to do is to add GCC 8 to your system PATH:

1 export PATH=/usr/local/gcc-8.1.0/bin:$PATH

the above will, temporarily, modify your PATH. If you want to make the change permanent, you need to add the above line at the end of your .bashrc file:

1 echo 'export PATH=/usr/local/gcc-8.1.0/bin:$PATH' >> .bashrc
2 source .bashrc

You can check if everything is properly setup by printing the version of the installed compiler:

1 gcc-8.1.0 --version

This is what I see on my Pi:

1 pi@raspberrypi:~ $ gcc-8.1.0 --version
2 gcc-8.1.0 (GCC) 8.1.0
3 Copyright (C) 2018 Free Software Foundation, Inc.
4 This is free software; see the source for copying conditions.  There is NO
5 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
6 
7 pi@raspberrypi:~ $

If the above worked with no error, you can do some clean up in order to save some space on your Pi:

1 cd ~
2 cd rm -r raspberry-pi-gcc-binary

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:

1 sudo rm -rf /usr/local/gcc-8.1.0

The above procedure will keep GCC 6.3 as the default C and C++ compiler for any package that depends on it. If you want to compile C programs you could use gcc-8.1.0 and for C++ g++-8.1.0.

Let’s try to compile and run a C++17 code that uses an if block with init-statement (the example is a bit silly, but it will show you how to compile C++17 programs):

 1 #include <iostream>
 2 
 3 int main() {
 4     // if block with init-statement:
 5     if(int a = 5; a < 8) {
 6         std::cout << "Local variable a is < 8\n";
 7     } else {
 8         std::cout << "Local variable a is >= 8\n";
 9     }
10     return 0;
11 }

Save the above code in a file named if_test.cpp and compile it with:

1 g++-8.1.0 -std=c++17 -Wall -pedantic if_test.cpp -o if_test

This is what I see on my Pi:

1 pi@raspberrypi:~ $ g++-8.1.0 -std=c++17 -Wall -pedantic if_test.cpp -o if_test
2 pi@raspberrypi:~ $ ./if_test
3 Local variable a is < 8
4 pi@raspberrypi:~ $

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 would recommend reading A tour of C++ by Bjarne Stroustrup.

or Effective Modern C++ by Scott Meyers.


Show Comments