GNU C++ is a compiler for the C++ programming language from the GNU Compiler Collection. It runs on Linux, Windows, and Mac OS. Its main purpose is to convert source code into an executable file (.exe) that can run on your computer.

A compiler reads source code (also known as “text”) written by the programmer and generates object files.

An object file contains machine language instructions that can be executed on your PC or Mac, along with information about symbols and types used in your program. A symbol is like giving some cool nicknames to certain parts of your source code.

The linker then combines all the object files together along with any library files you want, creating an executable file (.exe) – also known as ‘the program’. Depending on which libraries are linked, this executable may need more files to function correctly.

The compiler is usually divided into two parts: the front end and the back end. The front end analyzes and transforms the source code (from whatever human-readable language you’re using) into an intermediate representation and generates an initial pass of the back end.

In our case, the GNU C++ compiler compiles files in . c and .cpp extension. The GNU C++ compiler has a front end for C and another one for C++, which is why we usually say that GNU C++ can compile files in both languages or dialects.

There are several reasons why we use the GNU C++ compiler.

  • Code safety. One major reason for using GNU C++ Compiler is safety. A good compiler will help you find bugs in your code before anyone else does.
  • Portability. Another benefit of using the GNU C++ Compiler is portability. The programs compiled with GCC will work across different computers without needing to change a single line of source code.
  • Extensive standard library. A large collection of libraries comes as a part of GCC, and it’s one of the main reasons why we use it.

In this guide, we will show you how to install G++ compiler on an Ubuntu 20.04 system. After the installation, we’ll show you how to compile and run your first program using the G++ compiler. Let’s get started.


In order to follow this tutorial, you will need a working Ubuntu 20.04 system with root privileges.

It is advisable that you should either have a fresh installation of an Ubuntu OS and at least have 5GB of free drive space on your machine.

Step 1: Updating the System

The first thing that you should do before anything else is updating your system. To do so, run the following command.

sudo apt-get update -y

Running the Update command will check for package updates and download them for installation on your Ubuntu machine. Once the package has been downloaded, it will begin to install them.

Step 2: Installing G++ Compiler

Now that we have updated our system let’s install the g++ compiler. To do so, run the following command.Advertisement

sudo apt install build-essential manpages-dev -y

The build-essential is known as a meta-package. The build-essential package contains several tools that are needed for building programs. Including G++, GCC, make, GNU debugger.

The manpages-dev package is a collection of manual pages from the Linux Documentation Project, which includes the C and C ++ Programming Language Manuals. Using the manpages-dev package, you can view manual pages of both C and C++ programming languages.

To validate that the G++ compiler has been installed successfully, run the following command.

g++ --version

If you see the following output, then it means everything is working as intended.

g++ version

Step 3: Testing the G++ Installation

We shall now test the G++ installation. We will create a sample file that can be compiled using the G++ compiler from the terminal.

First, create a new file in your home directory called hello.cpp. To do so, run the following command.

sudo nano sample.cpp

Next, you’ll need to write some code inside this file. The syntax for doing so is as follows.


int main()
  printf ("Linuxprotip-Hello World!n");
  return 0;


This is a header file that is usually included, and it defines the input and output functions that we can use for this sample program.

printf (“Linuxprotip-Hello World!n”)

This is a function that prints the text string “Linuxprotip-Hello World!” to the standard output device, which in our case is the terminal.

return 0;

This line terminates the main() function and returns an integer status code to the program that called it.

Finally, we will print “Hello world!” on-screen, by including the following code.

int main() { }

We start with int main(). Then we have some brackets that contain our commands between them. In this case, we first want to specify that we want to print “Hello world!” on the terminal, so we use the printf function and pass the “Hello World” string as a parameter.

You’ll need to save the file and exit. Press Ctrl + O to save the file, and Ctrl + X to exit the nano text editor.

You can now compile your new file using G++. To do so, make use of the following command.

g++ sample.cpp -o sample

You’ll see no output on the terminal, just a blank screen when you run this command. This is fine, and it means that the program was successfully compiled and linked into a new executable file called sample.

Now, to run our executable file, we will use the following command.


If you see any error message(s), one of the possible sources is that your g++ compiler isn’t installed correctly or you are missing some crucial package. If everything goes smoothly, your terminal window should greet you with “Hello world!”.


In this guide, you have learned how to install the G++ compiler. As you can see, it’s pretty straightforward and easy to do by following the steps in this guide.

We hope that this article has taught you something new and you enjoyed reading it. See you next time.

How to Install GNU G++ (C++) Compiler and Toolchain on Ubuntu 20.04

Leave a Reply

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