Ubuntu

How to install g++ on Ubuntu

If you are a newbie in the world of computers and programming languages then it is important to know that your computer cannot “understand” any of the programming languages. The computers interpret only machine languages (ones and zeros). In this situation, a compiler can help you. A computer utilizes compilers for “translating” programming languages into machine language, or we can also say that it converts your source code into an executable file format for your system.

What is g++ in Ubuntu

The g++ is a GNU C++ compiler command utilized to create an executable file through compilation, pre-processing, linking, and assembling source code. There exist many options of the g++ command that permit us to stop the process at any point along the way.

In the next part of the article, we will explain how to install g++ in Ubuntu and use it to compile any C++ source file. So let’s begin!

Note: Login as root or superuser for installing packages and adding repositories to your system.

How to install g++ in Ubuntu

Now, we will check the method of installing g++ using the terminal. To do so, open your terminal in Ubuntu by pressing “CTRL+ALT+T”. Or by searching it manually in the Application’s search bar:

Update the repositories of your Ubuntu system by using the below-given command:

$ sudo apt update

Now, install g++ on your Ubuntu by writing out the following command in your terminal:

$ sudo apt install g++

Verify the existence of g++ on your system:

$ g++ --version

All done!

How to compile a C++ script with g++

Now, we will create a sample script, and by utilizing g++, we will compile it in the terminal. Use nano editor to create and edit the “samplefile.cpp” script:

$ nano samplefile.cpp

Now, add the following lines of code in this “samplefile.cpp” script:

#include <stdio.h>
int main()
{
  printf ("This is a test file\n");
  return 0;
}

Write out the code in the “samplefile.cpp” by pressing “CTRL+O”:

Press “CTRL+X” for exiting the nano editor. To run this “samplefile.cpp”, we have to convert “samplefile.cpp” into an executable “samplefile” file. For that, utilize g++ in this way :

$ g++ samplefile.cpp -o samplefile

Run the executable file “samplefile” in your terminal:

$ ./samplefile

That’s the method of compiling any C++ script using g++. Now, let’s discuss GCC and how you can use it to compile any C++ script.

What is GCC in Ubuntu

GCC is an acronym for GNU Compiler Collection. It is a group or collection of libraries and compilers for Fortran, D, C, C++, Ada, and Objective-C programming languages. GCC is used to compile many open-source projects, especially the Linux kernel and GNU utilities. It is an important component of the GNU toolchain. It is also considered a standard compiler for most Linux and GNU projects. In 2019, it was declared the most outstanding source project with around 15 million lines of code. GCC is an important tool in the development of free software.

With the help of GCC compilers, when you compile a source code file, the most critical argument to include is the source file’s name. Every other argument is an option, such as linking libraries, debugging, and warnings, etc. GCC commands permit its users to stop the process of compilation at various points. We always recommend the finest option for our readers. Go for GCC installation on your Ubuntu, as it has many libraries and compilers for programming languages, including C++.

How to install GCC in Ubuntu

A meta-package named “build-essential” exists in the default repositories of Ubuntu. This package comprises GCC compiler, utilities, and libraries that are needed for compiling any software. If you want to install GCC, write out the below-given command for adding the build-essential package to your system:

$ sudo apt install build-essential

Now, verify the existence of the GCC compiler:

$ gcc --version

How to compile a C++ script with GCC

Now, we will compile a “C++” file using the GCC compiler. For that, firstly, we will create a “testfile.cpp” script using the “nano” editor:

$ nano testfile.cpp

Now, add the following code in your “testfile.cpp” script. When we execute this script, it will print out “This is a test file” on the terminal.

#include <stdio.h>
int main()
{
  printf ("This is a test file\n");
  return 0;
}

Press “CTRL+O” to save the “testfile.cpp” script.

In this step, we will compile the “testfile.cpp” to an executable file “testfile” with the help of GCC:

$ gcc testfile.cpp -o testfile

Now, run the executable “testfile” C++ script:

$ ./testfile

It will show the following output:

Conclusion

Compilers are used for converting source code to an executable file format. Computers and many programming languages utilize these compilers. In Ubuntu, the GCC tool is used; it contains a collection of libraries and compilers for various programming languages, including C, C++, Ada. Whereas g++ is a GNU C and C++ compiler. We have shown you how to install g++ and GCC on your Ubuntu system. Moreover, examples are also demonstrated to explain how you can use g ++ and GCC to compile any C++ source file.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.