In this tutorial, we will learn the steps involved to run C++ program on a Linux machine. This is a step-by-step guide with screenshots and code.
- Linux machine
- C++ compiler
- vi Text Editor
Install GNU C++
In this step, we need to install the GNU C++ compiler.
Create C++ Source File
Open Terminal/ Konsole.
Change the working directory path to a suitable directory.
Create a file with .cpp file extension. For example, we will create a hello.cpp file.
$ touch hello.cpp
Edit the C++ source file
Type the following command
$ vi hello.cpp
Enter insert mode. Enter the “i” keyboard key.
Write C++ Program
Enter the C++ code. For simplicity, we will write a simple C++ program to print Hello, World! message to the computer screen.
Save the C++ file
Once done with the C++ code. Enter the esc key. Type wq and hit the Enter key to save the file and exit the vi text editor.
Compile C++ Program
To compile the C++ program type the following command:
$ g++ hello.cpp
This will create the a.out object file in the working directory.
Run C++ Program
To run the C++ program type the following command:
We should be able to see the message “Hello, World!” displayed on the computer screen.
a.out is the default executable output file. We can override this with the -o flag during the compilation process. To name the executable file:
$ g++ hello.cpp -o hello
That’s it. We have successfully created and run a sample C++ program on the Linux machine.
C++ Tutorials on this website:
For more information on the current ISO C++ standard