Compiler is a software program that converts the code written in high-level programming into a low-level machine-readable language. For example, the Linux compiler has various types, such as “AOOC”, “GHC”, “clang”, “G++”, and much more. The “G++” compiler is pre-installed on the latest version of Ubuntu 22.04. If it is not available, follow this article in which the installation method of G++ on Ubuntu is described in detail.
The content of this guide is illustrated below:
- How to Install G++ on Ubuntu?
- How to Execute the C++ Program Using G++ Compiler?
- Alternate Method: Install Build-Essential Package to Get G++ on Ubuntu
- How to Remove G++ From Ubuntu?
How to Install G++ on Ubuntu?
The main objective of the G++ compiler is to compile the C++ source code files into executable files, which can be executed from the Linux command line. This section demonstrates the step-by-step procedure to install the G++ compiler in the Linux distribution Ubuntu.
Step 1: Update the System Packages
First, press the shortcut key (Ctrl+Alt+T) to open the terminal on the desktop. Type the “apt update” command to update all the system packages:
$ sudo apt update
Step 2: Install the G++ Compiler
The “G++” compiler comes by default in Ubuntu 22.04, as shown in the below screenshot:
$ sudo apt install g++
If your Ubuntu system does not have the “G++” compiler then the output would be like shown in the following image:
$ sudo apt install g++
The output displays that the “G++” is successfully installed.
Once the installation is completed, check the “G++” compiler version by typing the below-mentioned command on the terminal:
$ g++ --version
The latest version of g++ “11.3.0” has been installed in Ubuntu 22.04.
How to Execute the C++ Program Using G++ Compiler?
The above section verifies that the “G++” is installed and is now ready for the compilation of the “C++” program. In this section, we will create a “C++” program and compile it using the “G++” compiler. Follow the step-by-step instructions to perform this task:
Step 1: Create a C++ Program
Ubuntu 22.04 offers the “nano file editor” that can be used to edit the files having shortcut keys for various tasks. Run the below-written command in the terminal to create the basic “myFirstProgram” C++ program:
$ nano MyFitrstProgram.cpp
The above command opens up the “nano editor ” window to write the program:
Save the program and press the “Ctrl+X” to exit the nano editor.
Step 2: Compile the Program
The next step is to compile the “myFirstProgram” C++ program by executing the command on the terminal:
$ g++ -o myFitrstProgram myFitrstProgram.cpp
Move to the “Files” icon in the dock panel and open it to check the saved C++ program. In the “Files” window, there is a “myFirstProgram” C++ program available in the “Home” directory:
After that, type the below-written command on the terminal to show the output of the “myFirstProgram” on the terminal:
$ ./myFirstProgram
The output“Its Linux Foss” is displayed on the terminal.
Alternate Method: Install Build-Essential Package to Get G++ on Ubuntu
This method is an alternative way to get G++ on Ubuntu. If the g++ is not available in the official repository, then first install the “build-essential” packages tool. To do so, run the following command on the terminal:
$ sudo apt install build-essential
The “build-essential” tool is successfully installed.
How to Remove G++ From Ubuntu?
The removal method of the G++ compiler is quite easy just by executing the “apt autoremove” command on the terminal:
$ sudo apt autoremove g++
Hence, the “G++” compiler has been removed.
Conclusion
On Ubuntu, the G++ compiler can be installed using the command “sudo apt install g++” or ”sudo apt install build-essential” package. On recent versions of Ubuntu, the G++ package comes by default. However, if not, you have learned the ways to install it on your Ubuntu system. Apart from that, you have also learned to get G++ on Ubuntu by installing the build-essential-packages.