Install and Use G++ on Ubuntu

While working as an application developer, it is common practice that some projects require different compiler versions for handling source code. With the rapid increase of software technology, you often find yourself where you need to use a specific compiler for a project. Today, different compiler versions are using for compiling the C programs. Here, we will talk about the G++ GNU compiler; a Linux system compiler specifically used to compile C++ programs. The file extensions of these programs are .c and .cpp that are compiled using the G++ compiler.

This article aims to provide a detailed guide on installing and using the G++ compiler on the Ubuntu 20.04 LTS system. You can achieve this goal by installing some development tools named build-essential packages on your system.

Installation of g++ on Ubuntu 20.04 system

Perform the below-mentioned steps to install g++ on the Ubuntu system:

Step 1: Install build-Essentials

The Ubuntu official default repository contains a number of development tools or packages. These tools you can install directly by installing a Ubuntu meta-package named “build-essential” on your system that includes the important development libraries, GNU debugger, and compiler collections required for compiling C/C++ applications. Therefore, it is required to install build-essentials on your system before installing any compiler. Type the following terminal command to install development tools:

$ sudo apt update

$ sudo apt install build-essential


The above meta-package installs the number of packages including g++ automatically, gcc, and make.

Step 2: Check g++ compiler version

Once all development packages are installed, type the following command to check the installed version of the g++ compiler:

$ g++ –version

The latest installed g++ version displays on the terminal as follows:

Install multiple g++ compiler versions on Ubuntu 20.04

First, install build-essentials development tools and then use the following command to install multiple g++ compilers:

$ sudo apt install build-essential

$ sudo apt -y install g++-7 g++-8 g++-9 g++-10


The above command will complete in a few minutes after installing all g++ compiler versions on your Ubuntu system.

Create a list of g++ alternatives using update-alternatives tools

The update-alternative tool is used to create the list of g++ multiple compiler alternatives. Using the below-mentioned commands, user can create the multiple g++ alternatives list on Ubuntu system:

$ sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-7 7

$ sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-8 8

$ sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-9 9

$ sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-10 10


Now, display the list of g++ alternatives on your Ubuntu system by using the following update-alternatives command:

$ sudo update-alternatives –config g++

Check the available g++ version in the list that would display on the terminal as follows:

Choose the g++ compiler version that you want to use as the default compiler for software development. Press ‘enter’ to keep the current choice set as default or select the number from the g++ alternatives list.

For example, to use the g++-9 version set as default, enter the number ‘4’. Hence, this version will be set as default. Similarly, if you want to switch to another g++ version then, repeat the configuration process and select the desired g++ version from the alternatives.

Installing g++ and gcc compiler in a single command

Most Linux users want to install multiple gcc and g++ compiler alternatives on the same system. In this case, you can also install gcc and g++ compilers using just a single command on your Ubuntu system.

$ sudo apt -y install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9


Follow the same procedure for configuring gcc compiler alternatives. Use the update-alternatives command to create the list of gcc compilers.

After that, select the default gcc version or alternative using the ‘config gcc’ command, which is also mentioned above.

$ sudo update-alternatives –config gcc

How to use of g++ on Ubuntu 20.04?

Compiling a C++ program using the g++ is quite easy and simple. We will create and execute the source code of the c++ program.

For example, to display a message ‘Hello, Welcome to the LinuxHint tutorials!’ create the following source code in a text file:

#include

using namespace std;

int main()

{

    cout “Hello, Welcome to the LinuxHint Tutorials!n;

    return 0;

}


Now, save the above source code and rename the file with ‘welcome.cc’. You can assign any name to this file with the ‘.cc’ extension.

Compile the above source code using the g++ compiler by running the following command:

$ g++ -o welcome welcome.cc

The above command creates a binary file named ‘welcome’ in the current directory where you run the command. Execute the ‘welcome’ executable program by using the following command:

$ ./welcome

The following message should display on the terminal after running the above command:

Conclusion

We learned about the installation of g++ on the Ubuntu 20.04 system in this article. We demonstrated how to install different g++ compiler versions at the same time on a Ubuntu system. Moreover, we have also explained how to compile a program using the g++ compiler. I hope all the above information about g++ is enough for your understanding. Visit GNU C compilers to get more details about Gcc or C compilers. Send us your feedback about this tutorial through comments.

Leave a Comment

Your email address will not be published. Required fields are marked *