Solarian Programmer

My programming ramblings

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

Posted on December 8, 2017 by Paul

Updated 10 May 2020

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

There is also a video version of this tutorial:

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

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

First, make sure that your Raspbian is updated:

1 sudo apt update && sudo apt upgrade -y

If you don’t have git on your Raspbian, you can install it with:

1 sudo apt install git

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

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

Next, extract the archive, move the extracted compilers to /opt and remove the repository:

1 cd raspberry-pi-gcc-binary
2 tar -xjvf gcc-10.1.0-armhf-raspbian.tar.bz2
3 sudo mv gcc-10.1.0 /opt
4 cd ..
5 rm -rf raspberry-pi-gcc-binary

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

 1 echo 'export PATH=/opt/gcc-10.1.0/bin:$PATH' >> ~/.bashrc
 2 echo 'export LD_LIBRARY_PATH=/opt/gcc-10.1.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
 3 . ~/.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-10.1, g++-10.1 or gfortran-10.1.

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

1 gcc-10.1 --version

This is what I see on my Pi:

1 pi@raspberrypi:~ $ gcc-10.1 --version
2 gcc-10.1 (GCC) 10.1.0
3 Copyright (C) 2020 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, at some point in the future, you’ll want to get rid of GCC 10 from your system, all you have to do is to remove the gcc-10.1.0 folder from /opt, example:

1 sudo rm -rf /opt/gcc-10.1.0

The above procedure will keep GCC 8.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-10.1 and for C++ g++-10.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>
 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++-10.1 -std=c++17 -Wall -pedantic if_test.cpp -o if_test

This is what I see on my Pi:

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