Install C++ on Linux in Less than 3 Minutes

Struggling to install C++ on Linux? 

Then, you landed on the right page. Here, you will get a complete process to run a code in C++ with the installation of g++ compiler in Linux. Here, we will guide you to write, compile, and run your own programs in the C++ programming language using terminal for Linux Operating System.

The terminal command prompt is pre-installed in your Linux operating system. Now, our job is to install the g++ compiler to code in the C++ programming in using the terminal.

Install c++ on linux

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Install C++ on Linux

You can easily install C++ on Linux, just follow these steps-

1. How to install g++ compiler using a terminal in Linux?

Our first step would be to open the terminal. The terminal icon might be on your desktop screen. If not, you can find it on the search menu. Another alternative to open terminal is a shortcut: Press Ctrl + Alt + T simultaneously.

  • This is what your command-prompt screen would look like.

Command-prompt screen on Linux

  • The second step would be to type the command: sudo apt -get install g++. It is important to note that g++ is the name of the C++ compiler.

 sudo command

  • If you don’t have the permission to install the g++ compiler, you would get a message: Enter the password to proceed further.

Enter the password to install g++ Compiler

  • If you have the permission to install the g++ compiler, a set of statements like this would appear. The installation would automatically begin.

installing g++ compiler on Linux

  • You would get a message: “Do you want to continue?” Press ‘y’ in order to continue. This is what your screen would look like after you press ‘y’.

Confirmation message to install G++

2. How to check the version of C++?

The following command would help you find out the version of the g++ compiler that you processed to install C++.

g++ — version

check the version of C++

After pressing enter, the version of the terminal software would be displayed:

The version is 7.4.0

If you don’t get such a message on your display screen, then it probably means that you haven’t successfully installed the g++ compiler.

3. How to create a C++ program?

In order to create a C++ program, use the ‘touch’ command and give the name of the file with the .cpp extension (Here, .cpp stands for C plus plus)

But, before that, you need to choose the directory you are working on.

Most of the programmers generally prefer to work on the Desktop folder as it proves to be quite convenient to write, modify and run your programs immediately.

In order to locate your files on the Desktop folder and to change the reference of the current directory to Desktop, use this command:

cd Desktop

Using cd Desktop

Key takeaway: Commands are case sensitive

The command for creating a program in C++ is:

touch program.cpp

Entering the command to create a program in C++

Now, a text file has been created in our Desktop folder called program.cpp

program.cpp (Linux Desktop)

You can now open this file and write a basic code – “Hello World!”

Hello World example of C++

Save your program after writing it in the text editor

4. How to compile and run C++ program?

We have now created a C++ program with the program name as program.cpp. It is now time to run and compile and run the code using the terminal.

The ‘g++’ command will help you throughout your coding journey. It is a very important command that comes pre-installed on your latest Ubuntu version.

To compile the code, we use the g++ command.:

g++ program.cpp -o program

Using g++ command to compile C++ code

Here, the g++ command is followed by the file name with the .cpp extension and ‘-o’ and the name of the executable file, which is, ‘program’ in this case.

Key takeaway: The executable file name can be different from the source file name

To run the program, use the command:

./program

Here, the period symbol ‘.’ followed by a backslash is followed by the name of the executable file, that is, ‘program’ in this case.

Output of Hello World example of C++

In this way, you can install C++ on Linux. 

Summary

In this tutorial, we discussed how to install the g++ compiler using the terminal when working on the Linux Operating System. We inferred that it is pretty easy to install the g++ compiler in Linux as it requires only one command. Thereafter, we saw how to check the version of the installed g++ compiler. Then, we saw how to create a program in C++ with the help of certain commands. Finally, we learned how to compile and run C++ programs using the terminal.

We hope you found this tutorial helpful in installing C++ using terminal for Linux! You may also like to know about the Unique Features of C++.

If you have any queries regarding the installation process, feel free to leave a comment below.

1 Response

  1. Pavan says:

    What is the RAM and HDD required for the C++ installation process ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.