How to Install GCC 14 and Use C++23

🚀 Several new features such as: std::print, std::println and others.


How to Install GCC 14 and Use C++23


GCC 14 has several new usability improvements. Although GCC 15 is already available, many systems still do not have it available in the package manager repositories, but GCC 14 does! Like in Ubuntu, for example.

If you are updating your system, but there are still no updates available, it may be because it is not the default yet, but you can install it and set it as the default for your system.

Let’s see how to do this.


Installation

To install, on Ubuntu, for example, just use APT:

First, update the repository list:

sudo apt update && sudo apt upgrade

Now just install GCC 14, in this case just g++:

sudo apt install g++-14

After installing, the version may not be up to date:

g++ --version
g++-13 (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0

However, it will already be available through the g++-14 command:

g++-14 --version
g++-14 (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0

Set as default for your system

To make it default when using only the g++ command, follow these steps:

  • 1. Add GCC 14 as an alternative:
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 100
  • 2. Now select interactively:
sudo update-alternatives --config g++

Choose the number relative to g++14, usually 2, they may both be the same, but force it by choosing 2.

After that, just run the g++ command without specifying:

g++ --version
g++ (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Advantages of doing it the way above instead of just changing the symbolic link:

  • Doesn’t break the system.
  • Can easily switch between versions.
  • Avoids conflicts with apt/dpkg.


Additional

If you are having problems with your LSP clangd also install these libraries for compatibility with Clang:

sudo apt install libstdc++-14-dev libc++-dev libc++abi-dev

Testing

If you create this code you will need to use the -std=c++23 flag to be able to compile, for example:

print.cpp

#include <print>

int main(){
  const std::string var{"All"}; 
  std::println("Hello, {}", var);
}

Compiling:

g++ -std=c++23 print.cpp

If you want to add -std=c++23 as default and not need to invoke it when compiling, you can add an alias to ~/.bashrc, for example:

To get the full path of your g++ use the command which, e.g.: which c++

echo 'alias g++="/usr/bin/g++ -std=c++23"' >> ~/.bashrc
source ~/.bashrc

There you go, now you don’t even need -std=c++, just run: g++ print.cpp.

Another way is to use specs, for example:

Generate the specs:

g++ -dumpspecs > ~/.config/specs

Edit the specs and change the relative line and below the content: *cpp:, replace everything there and leave it like this:

*cpp:
%{posix:-D_POSIX_SOURCE} -std=c++23

And then add this to your ~/.bashrc:

export GCC_SPEC_FILE=/path/to/specs
alias g++="g++ -specs=$GCC_SPEC_FILE"

You can use the same logic for the command: gcc for the C Language.

For more information, visit the links below:


cpp gcc


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles