Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Install g++ on Ubuntu
2.1.
Compile a C++ script with g++
3.
GCC in Ubuntu
3.1.
Install GCC in Ubuntu
3.2.
Compile a C++ script with GCC
4.
Frequently Asked Questions
4.1.
Which compiler is best to use C++ in Ubuntu?
4.2.
What is the main difference between gcc and G++?
4.3.
How to know if the user has installed a compiler in Linux?
4.4.
Among all the compilers of c++, which is the fastest one in Ubuntu?
5.
Conclusion
Last Updated: Mar 27, 2024

How to install the C++ compiler on Ubuntu

Author Naman Kukreja
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

If you are reading this blog, then it means you have used a computer or smartphone in your life, so have you ever wondered how they respond to our command and communicate within themselves and show us the required result or perform the operation necessary?

If you are a newbie or beginner in the world of programming, then you must know that computers cannot understand the languages as we do. They only understand the machine language, i.e., binary language( only 0 and 1). The request we make is converted into machine language and then sent and understood by the computer.

And Compiler help in translating your request into machine language to get you the required result. We will learn more about compilers and how to install a C++ compiler in Ubuntu while moving further with the blog. So without wasting any further time, let's get on with our topic.

Also, see Literals in C.Fibonacci Series in C++ and,Lexical Analysis in Compiler Design

Install g++ on Ubuntu

A compiler can be understood as a program that converts your input into machine language to get the required result.

In this blog section, we will install g++ is, referred to as a C++ compiler command that can be utilized to create an executable file through linking, pre-processing, compilation, and assembling source code. With the help of g++ commands, we can stop the process according to our requirements. You need to follow certain steps to install g++, which are explained below:

  1. Open terminal in Ubuntu “Ctrl+alt+T” . OR you can search it manually ib the application’s bar


Source

2. Now update all the repositories by running the following command in the terminal.

$ sudo apt update

3. Now install g++ by running the following command in the terminal:

$ sudo apt install g++

4. At last, verify that g++ is installed on your system or not by running the following command in the terminal. If g++ is present, it will show the latest version of it. Otherwise, it will not show anything. Then you can install g++ first.

$ g++ --version

Compile a C++ script with g++

In this blog section, we will create a sample script and compile it using g++ in the compiler. We will use a nano editor to create a “samplefile.cpp” by running the following script.

$ nano samplefile.cpp

Now you can add the code given below in your file and save by pressing "CTRL + O"

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

The screen will look like this:

Source

Before running, we have to convert this file into an executable file by running the following script in your terminal:

$ g++ samplefile.cpp -o samplefile

Now, you can run your file in the terminal by executing the following command:

$ ./samplefile

The output will be as follows:

Source

Also Read - C++ Interview Questions And Abstract Data Types in C++

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

GCC in Ubuntu

GCC is referred to as an acronym for GNU Compiler Collection. While running different programming languages in the compiler, we will need some libraries to make our conversion easy. GCC refers to a set of libraries and compilers for languages like C++, C Fortran, Objective-C, and Ada. It can compile open-source projects like GNU utilities and Linux Kernel. GCC is often considered a standard compiler for GNu projects and Linux.

When using GCC compilers to compile a source code file, the source file's name is the most important input to include. Other arguments, such as linking libraries, warnings, and debugging, are optional. GCC instructions allow users to halt the compilation process at specific points. For our readers, we always offer the best solution. GCC offers various compilers and libraries for programming languages, including C++. Therefore install it on Ubuntu.

Read More, Phases of compiler

Install GCC in Ubuntu

Follow the steps given below to install GCC in Ubuntu:

  1. First, we need to download the “ build-essential” package as it will contain all the utilities, GCC compiler, and compilers required for compiling any software. Run the following command in your terminal:
$ sudo apt install build-essential

2. After downloading the GCC compiler, verify its existence by running the following command in your compiler. If GCC compiler exists, it will show the latest version. Otherwise, you need to install it again.

$ gcc --version

Compile a C++ script with GCC

In this blog section, we will compile a C++ file using the GCC compiler. Follow the steps given below to compile the file:

  1. First, we will create a testfile.cpp in the nano editor by running the following command:
$ nano testfile.cpp

2. Now copy the given code in testfile.cpp and press “CTRL + O” to save the file.

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

Source

3. We need to make the file executable first, so to do so, run the following command:

$ gcc testfile.cpp -o testfile

4. Now run the executable file by running the following command. The output will be as follows:

$ ./testfile

Source

You can also do a free certification of Basics of C++.

Also read -  File Handling in CPP

Frequently Asked Questions

Which compiler is best to use C++ in Ubuntu?

The standard GCC compiler( GNU Compiler Collection) is the most used compiler as it is free and provides the user with many features.

What is the main difference between gcc and G++?

They both are compilers, but the difference is that gcc is used to compile C programs, whereas G++ is used to compile C++ programs.

How to know if the user has installed a compiler in Linux?

You can check it by running the "gcc-version" command. It will tell you whether your compiler is installed or not.

Among all the compilers of c++, which is the fastest one in Ubuntu?

Zapcc is the fastest among all the c++ compilers in Ubuntu in compile tests.

Conclusion

In this article, we have extensively discussed the compiler and steps to download and install the C++ compiler in Ubuntu with step by step explanation.

We hope this blog has helped you enhance your knowledge of compilers and the steps to download them in Ubuntu. If you want to learn about setting a node server in Ubuntu, you must refer to this blog here, and you will get a complete idea and step-by-step explanation of the same.

Also read, Turbo C++,  ping command in linux

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, React, JavaScript, System Design, etc. Enroll in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations. Do upvote our blog to help other ninjas grow.

 “Happy Coding!”

Previous article
Diamond Problem in C++
Next article
Constructor in C++
Live masterclass