Installing C++17 and C++20 on Ubuntu and Amazon Linux

The standard installation of gcc and g++ does not come with the latest versions of the libraries, so if you want to take advantage of the features offered by the new C++ standards (C++17 and C++20 as the time of writing) you have to manually switch libraries.

Specifically, you have to make sure that the libraries gcc and libstdc++ are upgraded to their latest versions. As the time of writing, those are the followings:

  • gcc-10
  • gcc-10-base
  • gcc-10-doc
  • g++-10
  • libstdc++-10-dev
  • libstdc++-10-doc

On Ubuntu that is rather trivial. First, look if they are already installed :

apt list --installed gcc-10* 
apt list --installed g++-10
apt list --installed libstdc++-10*

If not, first check if they are available to apt:

sudo apt-get update
apt list libstdc++-10*
apt list gcc-10*
apt list g++-10*

If you do not find the packages, you might need to run the following commands to add the Ubuntu Toolchain repository:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update

Once updated the apt, you can install them with the following command:

sudo apt install gcc-10 gcc-10-base gcc-10-doc g++-10
sudo apt install libstdc++-10-dev libstdc++-10-doc 

Then you need to tell the compiler to use the C++20 standard by using the -std=c++20 flag. Beware also of the experimental nature and continuous evolution of the support for the standards still under development.

A similar procedure can be used to install gcc-10 and libstdc++10 on Amazon Linux. You must first identify on rpmfind.net the correct packages for gcc and libstdc++ . Those packages are usually part of a Fedora Project repository (testing or raw). If you need to add a repository, you can follow the steps reported in the Fedora Yum guide or the guide for adding repositories in the Amazon documentation. Also keep in mind that names and numbering might differ slightly from the ones available in Ubuntu. Once you have located the correct rpm, you can install it with the following command:

sudo yum install link-to-package
C++ 

See also