Create your first program with C++ on Ubuntu 22.04 or Debian 11
C++ is a high-performance and generally compiled programming language. It combines object-oriented programming features and low-level programming, making it suitable for a wide range of applications, from software development to embedded systems or even games.
In this manual, we will teach 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 our Operating System's console.
The first step will be to update the package list and their versions to 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 the version of your compiler with the following command:
gcc --version
Once the installation is finished, you must create the file where we will write the program's 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 will be to open the file with your preferred editor. We will do it with the nano editor:
nano helloWorld.cpp
The program code will be the following:
#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 named helloWorld
And finally, all that remains is to execute it:
./helloWorld
We will see how the text Hello World! appears on our console screen. This means that the program has been executed correctly.