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:
If you don’t have git on your Raspbian, you can install it with:
Let’s start the GCC installation process. Open a Terminal and download a binary of GCC 10:
Next, extract the archive, move the extracted compilers to /opt and remove the repository:
Next, we are going to add the new compilers to the path and create a few symbolic links:
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:
This is what I see on my Pi:
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:
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):
Save the above code in a file named if_test.cpp and compile it with:
This is what I see on my Pi:
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.