by marcuz-apl

Intro

This article will showcase how to install the GCC Compiler Collection of C, C++ and Object-C.

Pre-requisites

  • Ubuntu 22.04 + installed
  • You are in root user or a user with sudo privilege.

Step 1 - Install GCC on Ubuntu

Let's get hands dirty.

## Update the system
sudo apt-get update
## Ensure teh essential package are in place
sudo apt install build-essential
## Install manpages-dev, which includes the g++ make
sudo apt install manpages-dev
## Check out the version of G++
gcc --version

Hola, You got GCC installed in the system.

## Ubuntu 22.04
gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0
Copyright (C) 2021 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.

## Ubuntu 24.04
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 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.

Step 2 - Compile a "Hello World" app

Spot on - create a simple source code in C by using a text editor:

mkdir c-apps
cd c-apps

nano hello.c

The content of that C program:

#include <stdio.h>
int main()
{
  printf ("Hello, welcome to the real World!\n");
  return 0;
}

then compile it.

gcc hello.c -o hello

then run it.

./hello

The output belike.

Hello, welcome to the real World!

Step 3 - Install multi-version GCC

New version has better functions and optimizations, then give a try.

## The default version of GCC on Ubuntu 22.04 is 11, but it supports 9, 10, 11 and 12.
sudo apt install gcc-10 g++-10 gcc-12 g++-12
## The default version of GCC on Ubuntu 24.04 is 13, but it supports 9, 10, 11, 12, 13 and 14.
sudo apt install gcc-12 g++-12 gcc-14 g++-14

Step 4 - Alternate the GCC versions

Now, prioritize the versions by updating the alternative pool.

Please note: the number after the gcc-12

## Ubuntu 22.04
### add gcc-12 - the newly installed
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12
### add gcc-11 - the default version
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
### add gcc-10 - the newly installed
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
## Ubuntu 24.04
### add gcc-14 - the newly installed
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 140 --slave /usr/bin/g++ g++ /usr/bin/g++-14 --slave /usr/bin/gcov gcov /usr/bin/gcov-14
### add gcc-13 - the default version
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130 --slave /usr/bin/g++ g++ /usr/bin/g++-13 --slave /usr/bin/gcov gcov /usr/bin/gcov-13
### add gcc-12 - the newly installed
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12

At this moment, the default shall be the latest version: 12 for Ubuntu 22.04 and 14 for Ubuntu 24.04

gcc --version

Then, take a look at what we have in the pool:

sudo update-alternatives --config gcc

You should have the following outputs:

## Ubuntu 22.04
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-12   120       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-11   110       manual mode
  3            /usr/bin/gcc-12   120       manual mode
Press <enter> to keep the current choice[*], or type selection number:

## Ubuntu 24.04
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-14   140       auto mode
  1            /usr/bin/gcc-12   120       manual mode
  2            /usr/bin/gcc-13   130       manual mode
  3            /usr/bin/gcc-14   140       manual mode

Press <enter> to keep the current choice[*], or type selection number:

From there, you could type in the number (1, 2, or 3) to select your preferred version of GCC.

Conclusion

Great to have plan B, isn't it?