Here is a step by step detailed guide on showing you how to write and compile a C++ program in Linux. For demonstration purposes, I’ll show you how to write and compile Hello World which is typically the first program that many students learn when introduced to programming.
How to write and compile Hello World in C++
First, let’s locate and open up our handy terminal. The terminal is the console on where you will initially learn how to create folders in order to create the programs that we will later compile. Depending on your OS your Terminal or Console might be opened in various ways. The image below demonstrates how to open it in Ubuntu.

Installing the appropriate tools
We might have to install the tools necessary to start writing, and compile our code. In order to do this we must specify some commands to install our tools. If you already have the programs installed, it will either upgrade them, or do nothing so either way there is no harm in trying this out.
The following commands simply give permission to install the packages we need.
sudo apt-get install g++ sudo apt-get install emacsIf it asks you for a password, simply provide the password you log in with. And if it asks you for what version of emacs, simply type in emacs22 for now.
Other linux distributions may have different methods for installing these tools. If so, use their Package Managers to download them.
Creating a folder and creating the Hello World program file
Now, let’s create a new folder and open a file in that folder to write our Hello World program. I’ll explain the commands shown on the screen in more detail below the image.

The following command lists the contents of the current directory.
lsThe following command makes a directory with the name FOLDERNAME.
mkdir FOLDERNAMEThe following command changes your current directory to FOLDERNAME.
cd FOLDERNAMEThe following command opens up the program emacs with the file hello_world.cpp. If not created it will create it for you.
emacs hello_world.cpp
Writing Hello World
With emacs open, simply click on the editor and it should turn blank. Write the following code for your Hello World program. When done, simply hit save. (It will save in the folder you called emacs hello_world.cpp)
1 2 3 4 5 6 7 8 | #include <iostream> using namespace std; int main() { cout << "Hello World!\n"; return 0; } |

Your editor should look like the image above.
Compiling our Hello World Program
Now that you have your program, let’s compile it so it can be written in machine language that can later be executed.

The following command will compile your program and create an executable called a.out.
g++ hello_world.cppThe following command will execute your program.
./a.out
Congratulations! You are done! I tried making it as simple as possible. If you still have any questions, feel free to drop a comment or suggestion to make this tutorial better!
Useful resources
C++ Guide Your guide for programming in C++
