Categories: 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.

Sponsored

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
int main()
{
printf (“This is a test filen);
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

Sponsored

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
int main()
{
printf (“This is a test filen);
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.

Ubuntu Server Admin

Recent Posts

Ubuntu Command-Line Mastery: The Essential Guide for Power Users

The Linux terminal is a powerful tool allowing users to control their system precisely and…

4 hours ago

Ubuntu Weekly Newsletter Issue 876

Welcome to the Ubuntu Weekly Newsletter, Issue 876 for the week of January 19 –…

3 days ago

How to utilize CPU offloads to increase storage efficiency

Canonical Ceph with IntelⓇ Quick Assist Technology (QAT) Photo by v2osk on Unsplash When storing…

3 days ago

Breaking the Rules: RPC Pattern with Apache Kafka and Karafka

Introduction Using Kafka for Remote Procedure Calls (RPC) might raise eyebrows among seasoned developers. At…

4 days ago

How to Install PalWorld on Ubuntu VPS (Easy 5 Minute Guide)

This article provides a guide for how to install PalWorld on Ubuntu VPS server. How…

5 days ago

How to Resolve Unmet Dependencies Error on Ubuntu

Using APT to manage software on Ubuntu (or similar Linux systems) is generally simple. It…

6 days ago