Solarian Programmer

My programming ramblings

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

Posted on December 8, 2017 by Paul

Updated 4 May 2019

In this article I will show you how to install GCC 9 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 8 on your Raspberry Pi, check my article.

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

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

1 git clone

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

1 cd raspberry-pi-gcc-binary
2 tar xf gcc-9.1.0-armhf-raspbian.tar.bz2
3 rm gcc-9.1.0-armhf-raspbian.tar.bz2
4 sudo mv gcc-9.1.0 /opt

Next, we are going to add the new compilers to the path and create a few symbolic links:

 1 echo 'export PATH=/opt/gcc-9.1.0/bin:$PATH' >> ~/.bashrc
 2 echo 'export LD_LIBRARY_PATH=/opt/gcc-9.1.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
 3 source ~/.bashrc
 4 sudo ln -s /usr/include/arm-linux-gnueabihf/sys /usr/include/sys
 5 sudo ln -s /usr/include/arm-linux-gnueabihf/bits /usr/include/bits
 6 sudo ln -s /usr/include/arm-linux-gnueabihf/gnu /usr/include/gnu
 7 sudo ln -s /usr/include/arm-linux-gnueabihf/asm /usr/include/asm
 8 sudo ln -s /usr/lib/arm-linux-gnueabihf/crti.o /usr/lib/crti.o
 9 sudo ln -s /usr/lib/arm-linux-gnueabihf/crt1.o /usr/lib/crt1.o
10 sudo ln -s /usr/lib/arm-linux-gnueabihf/crtn.o /usr/lib/crtn.o

At this point, you should be able to invoke the compilers with gcc-9.1, g++-9.1 or gfortran-9.1.

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

1 gcc-9.1 --version

This is what I see on my Pi:

1 pi@raspberrypi:~ $ gcc-9.1 --version
2 gcc-9.1 (GCC) 9.1.0
3 Copyright (C) 2019 Free Software Foundation, Inc.
4 This is free software; see the source for copying conditions.  There is NO
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 9 from your system, all you have to do is to remove the gcc-9.1.0 folder from /opt, example:

1 sudo rm -rf /opt/gcc-9.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-9.1 and for C++ g++-9.1.

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>
 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++-9.1 -std=c++17 -Wall -pedantic if_test.cpp -o if_test

This is what I see on my Pi:

1 pi@raspberrypi:~ $ g++-9.1 -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

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