In this tutorial, you’ll learn in Detail how to Run C++ Program on Linux.
To Run C++ Program on Linux, you need:
The below mentioned three things which are important to Run C++ programming on Linux.
- a compiler. We’ll install GNU GCC compiler which is good for beginners.
- development tools and libraries.
- a text editor (gEdit works just fine for our purpose). Or, you can download text editor of your choice.
Here’s a step by step guide to run C++ in Linux (Ubuntu, Debian, CentOS, Fedora, Redhat and Scientific Linux):
1. Open the terminal and issue the following command.
For Ubuntu and Debian distribution:
$ sudo apt-get update $ sudo apt-get install build-essential manpages-dev
For CentOS, Fedora, Redhat and Scientific Linux:
# yum groupinstall 'Development Tools'
This installs GNU GCC compiler and related tools on your system.
To verify if GCC compiler is installed, issue the command
If you get output similar like this, gcc is correctly installed on your system.
2. Open the text editor of your choice and save a file with .cpp extension. I made hello.cpp file using gEdit.
If you are a Linux wizard, feel free to use vim or emacs. Any editor is fine but, don’t forget to use .cpp extension; it’s important.
3. Switch to the directory where the file is located. And, issue the following command.
$ g++ program-source-code.cpp-o name-of-your-choice
Here, program-source.code.cpp is the filename you chose before. And, name-of-your-choice can be any name you prefer. In my case, I issued the following command.
$ g++ hello.cpp -o hello
If there is no error, an executable file named is created; hello is my case.
Finally, you can see the output using the following command.
As you might have imagined, you need to use the name of the executable file you chose before. Also, you need to use the path to the execute the file if you are in a different directory.