Site icon DataFlair

Install C++ on Linux in Less than 3 Minutes

Free C++ course with real-time projects Start Now!!

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

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.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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

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

Key takeaway: Commands are case sensitive

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

touch program.cpp

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

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

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

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.

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.

Exit mobile version