Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

Create your first C++ program on Ubuntu 22.04 or Debian 11

C++ is a high-performance, generally compiled programming language. It combines object-oriented programming and low-level programming features, making it suitable for a wide range of applications, from software development to embedded systems or even games.

In this manual we will show you how to create your first program using C++ in Ubuntu 22.04 or Debian 11, where we will display the text "Hello World!" through the console of our Operating System.

The first step will be to update the list of packages and their versions and then install the build-essential development package to be able to compile our applications made in C++.

sudo apt update
sudo apt install build-essential -y

You can check your compiler version with the following command:

gcc --version

Once the installation is finished, you must create the file where we will write the program code. In our case we will call it helloWorld.cc. You can create the file with the following command:

touch helloWorld.cpp

The next step is to open the file with your favorite editor. We will do it with the nano editor:

nano helloWorld.cpp

The program code will be as follows:

#include <iostream>
using namespace std;

// Your first program in C++
int main()
{
        cout << "Hello World!" << endl;
        return 0;
}

Once the file is saved, we must compile our program with the following command:

g++ -o helloWorld helloWorld.cpp

This command will create an executable file with the name helloWorld.

And finally we only have to run it:

./helloWorld

We will see on our console screen the text Hello World!. This means that the program has been executed correctly.