How to install g++ in ubuntu

How to Install g++ in Ubuntu

Before we get started, you need to understand that a computer cannot directly give commands to a computer to execute. A computer only understands ones and zeros. If you want your computer to understand and execute what you want it to do, you need to program certain commands using a high-level programming language such as C++, etc. But wait, you cannot just write C++ code and expect your computer to execute the commands on its own, you need a compiler to convert your code into machine language, which your computer can easily understand. Or simply put, a compiler converts your code into executables for your operating system.

Sponsored

Not knowing about a tech ecosystem can make even the smallest tasks seem cumbersome. For anyone starting with their programming journey, getting the environment set up is the biggest task. In Windows you get all-encompassing solutions that set up everything for you, such isn’t the case on Ubuntu. While there are PPAs and packages, setting up a programming environment for C++ isn’t as simple. So, let’s dive deep into it and learn how to install g++ on Ubuntu 20.04.

What is g++?

g++ is a compiler for Linux based systems, used to compile C++ programs. It can compile files with both “.c” and “.cpp” file extensions. g++ creates the executable by compiling the file, pre-processing, linking and then ultimately assembling the source code. It comes loaded with a plethora of commands to handle our C++ code.

See also  How to Screen Capture in Ubuntu 22.04?

Installing g++

As always, we will be utilizing the terminal for the installation process. You can fire it up either through the applications search bar as such:

How to install g++ in ubuntu 1

Or if you want a quicker way, you can go with pressing “Ctrl + Alt + T” on your keyboard. Now proceed to update the system repositories through our trusty old command:

$sudo apt update

How to install g++ in ubuntu 2

After this, you are ready to install g++, with:

$sudo apt install g++

How to install g++ in ubuntu 3

After confirming that you are willing to give up 121MB of your HDD space to proceed with the installation, the installation will proceed and complete depending on the speed of your computer.

Verifying the installation

Once the installation has been completed, you can verify the installation by running the following:

$g++ --version

How to install g++ in ubuntu 4

This shows you the version and confirms that the g++ compiler has been installed on your Ubuntu.

Compiling C++ with g++

Now that we have the compiler installed, let’s take a look at the steps you need to do for compiling a C++ program.

Let’s use the nano editor to create our C++ code file.

Sponsored
$nano helloWorld.cpp

How to install g++ in ubuntu 5

Here helloWorld is the name of our file. You can keep the name of the file as you like. Now open up the file and let’s type the following basic C++ code. It’s almost like a ritual that anyone who starts programming starts with a Hello World program.

#include 

 int main ()

 {

                cout<<”Hello World”;

                return 0;

 }

How to install g++ in ubuntu 6

Here we are just displaying “Hello World” on the terminal. Save the file and exit out of it by pressing “Ctrl + X”. Now we are going to compile this with the following:

$g++ helloWorld.cpp -o helloWorld

How to install g++ in ubuntu 7

g++ takes up time to compile your application depending on the amount of code in your file. In my case, it was done instantaneously. After running this, you will see a new file is created in your current directory.

How to install g++ in ubuntu 8

Here the .cpp file is the one with the code and the one without any file extension is the executable. Now we run the newly created executable file, through this:

$./helloWorld

How to install g++ in ubuntu 9

As you can see the terminal has our coded text displayed right before the prompt. This shows that we have been successful in achieving our objective of setting up the g++ compiler and have succeeded in compiling and then executing our first C++ code on Ubuntu.

Conclusion

C++ is a very popular programming language for back-end development. It’s an awfully fast and even more so efficient language. Many of the industry-leading tools are leveraging C++ at their back-end.

If you have followed all of these steps carefully, you are now ready to start writing C++ code on your Ubuntu machine. If you run into issues with any of these commands, or if you want to know more about programming in C++, feel free to drop a message below, and let’s have a chat.

Karim buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply