How to install g ++ on Ubuntu

If you are new to the world of computers and programming languages, it is important to know that your computer is not “to understand“One of the programming languages. The computers only interpret machine languages ​​(ones and zeros). In this situation a Compiler can help you. A computer uses compilers for “translate“Programming languages ​​in 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?

g ++ is a GNU C ++ compiler command used to create an executable file by compiling, preprocessing, linking, and assembling source code. There are many options to the g ++ command that allow us to stop the process at any time.

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 start!

Note: Log in as root or superuser to install packages and add repositories to your system.

How to install g ++ in Ubuntu

Now we are going to review the method of installing g ++ through the terminal. To do this, open your terminal in Ubuntu by clicking on “CTRL + ALT + T“. Or by manually searching in the application’s search bar:

Update your Ubuntu system’s repositories using the command given below:

$ sudo apt update

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

$ sudo suitable To install g ++

Check the existence of g ++ on your system:

$ g ++ –Execution

All done!

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

Now let’s create a sample script and compile it with g ++ in the terminal. Use Nano editor to create and edit the “Sample file.cpp“Script:

$ Nano Sample file.cpp

Now add the following lines of code to this “Sample file.cpp“Script:

#include
int mainly()
{
printf (“This is a test file n);
return 0;
}

Write the code in the “Sample file.cpp“by pressing”CTRL + O“:

Press “CTRL + X”To exit the nano editor. To do this “Sample file.cpp“, We have to convert”Sample file.cpp“Into an executable file”Sample file“File. To do this, use g ++ this way:

$ g ++ Sample file.cpp Sample file

Run the executable file “Sample file“In your terminal:

$ ./Sample file

This is the method for compiling any C ++ script with 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 Compiler to the Fortran, D, C, C ++, yes, and Goal c Programming languages. GCC is used to compile many open source projects, particularly the Linux kernel and the GNU utilities. It is an important part of the GNU toolchain. It is also considered the standard compiler for most Linux and GNU projects. In 2019, it was declared the most outstanding source project with around fifteen Millions of lines of code. GCC is an important tool in free software development.

Using GCC compilers, when compiling a source code file, the main argument to include is the name of the source file. Any other argument is an option, such as library linking, debugging and warnings, etc. GCC commands allow its users to stop the compilation process at various points. We always recommend the best option to our readers. Go to the GCC installation on your Ubuntu as it has many libraries and compilers for programming languages ​​including C ++.

How to install GCC in Ubuntu

A metapackage called “construction-essential“Exists in the standard Ubuntu repositories. This package contains the GCC compiler, utilities, and libraries needed to compile software. If you want to install GCC, write the command given below to add the Build Essential package to your system:

$ sudo suitable To install construction-essential

Now check the existence of the GCC compiler:

$ gcc –Execution

How to Compile a C ++ Script Using GCC

Now let’s create a “C ++”File with the GCC compiler. To do this, we first create a “testfile.cpp“Script with the”Nano“Editor:

$ Nano testfile.cpp

Now add the following code to your “testfile.cpp“Script. When we run this script, it will print”This is a test file“At the terminal.

#include
int mainly()
{
printf (“This is a test file n);
return 0;
}

Press “CTRL + O“To save the”testfile.cpp“Script.

In this step we create the “testfile.cpp“Into an executable file”Test file“With the help of GCC:

$ gcc testfile.cpp Test file

Now run the executable file “Test file”C ++ script:

$ ./Test file

It shows the following output:

diploma

Compiler are used to convert source code to an executable file format. Computers and many programming languages ​​use these compilers. Under Ubuntu is 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 showed you how to do it g ++ install and GCC on their Ubuntu System. Examples are also demonstrated to explain how you can compile any C ++ source file using g ++ and GCC.

Related Posts