cpp standard and compiler
To use update-alternatives to manage your g++ versions, you must first ensure a newer compiler (like GCC 11) is installed, register both versions with the system, and then select the new default. Here is the step-by-step process to set it up and verify it with a test compilation.
GCC to Default C++ Standard Mapping
| G++ Version | Default C++ Standard Flag | Base ISO Standard |
|---|---|---|
| G++ 16 | -std=gnu++20 |
C++20 |
| G++ 11 to 15 | -std=gnu++17 |
C++17 |
| G++ 6 to 10 | -std=gnu++14 |
C++14 |
| G++ 5 and older | -std=gnu++98 |
C++98 / C++03 |
Install g++
Step 1: Install a newer compiler and register both versions
Run the following commands to install g++-11 and add both your current g++ and the new g++-11 to the update-alternatives system.
# 1. Add the toolchain test repository
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
# 2. Update your package lists to pull in the new repository data
sudo apt update
# 3. Install GCC 11
sudo apt update && sudo apt install g++-11 -y
# 4. Register your current system g++ (typically pointing to g++-9 on Ubuntu 20.04)
# We assign it a priority of 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 50
# 5. Register the new g++-11 with a higher priority of 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
# 6. Register the new g++-16 with a higher priority of 150
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-16 150
(Note: If your original compiler is not version 9, replace /usr/bin/g++-9 above with your actual version, which you can find by running ls /usr/bin/g++*).
Step 2: Choose your default version
If you ever want to manually toggle back and forth between them later, run this command:
sudo update-alternatives --config g++
You will see a menu like this.
Type the selection number for g++-11 and press Enter:
text
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/g++-11 100 auto mode
1 /usr/bin/g++-9 50 manual mode
2 /usr/bin/g++-11 100 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Step 3: Verify by checking the macro and compiling
Now, let's verify that your system defaults to the new version and can natively compile C++20 without changing any flags.
Check the new default macro standard
Run your macro query again. It should now output 201703L (C++17) as the bare default:
echo | g++ -dM -E -x c++ - | grep __cplusplus
• 202002L indicates C++20
• 201703L indicates C++17
• 201402L indicates C++14
• 201103L indicates C++11
Test a C++20 compilation using the standard flag
Because g++-11 recognizes the finalized standard, your previous one-liner with -std=c++20 will now compile and execute flawlessly:
echo -e '#include <iostream>\nint main() {\n std::cout << "Hello from C++20!" << std::endl;\n return 0;\n}' > main.cpp && g++ -std=c++20 main.cpp -o program && ./program
Test a C++20 compilation using c++20 feature:
echo -e '#include <iostream>\n#include <concepts>\n\ntemplate<typename T>\nconcept Numeric = std::integral<T> || std::floating_point<T>;\n\nvoid print_val(Numeric auto x) {\n std::cout << "Native C++20 Concept Works! Value: " << x << std::endl;\n}\n\nint main() {\n print_val(2026);\n return 0;\n}' > main.cpp && g++ main.cpp -o program && ./program