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:
Now, install g++ on your Ubuntu by writing out the following command in your terminal:
Verify the existence of g++ on your system:
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:
Now, add the following lines of code in this “samplefile.cpp” script:
printf ("This is a test file\n");
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 :
Run the executable file “samplefile” in your terminal:
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:
Now, verify the existence of the GCC compiler:
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:
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.
printf ("This is a test file\n");
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:
Now, run the executable “testfile” C++ script:
It will show the following output:
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.