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.
Perform the below-mentioned steps to install g++ on the Ubuntu system:
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 install build-essential
The above meta-package installs the number of packages including g++ automatically, gcc, and make.
Once all development packages are installed, type the following command to check the installed version of the g++ compiler:
The latest installed g++ version displays on the terminal as follows:
First, install build-essentials development tools and then use the following command to install multiple g++ compilers:
$ 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.
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++-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:
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.
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.
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.
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:
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:
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:
The following message should display on the terminal after running the above command:
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.
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…
Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…