I am going to show you how to compile a C++ program on Linux. I’ll demonstrate it on Ubuntu Desktop 23.04, I’ll use the tried and true Hello, World! app as an example.
The first thing you must do is install the necessary software to allow you to build C++ programs. To do that, open a terminal window and issue the command sudo apt-get install build-essential -y. If you’re on a Fedora-based distribution, that command would be sudo dnf install gcc-c++ -y.
Now that you have the necessary tools installed, let’s create the Hello, World! file with the command nano hello.cpp. In that file, paste the content shown in the video. Once you’ve pasted the content into the file, save it with the Ctrl-X shortcut.
Next, we have to compile the program with the command g++ hello.cpp -o hello. Because this is such a small program, it should compile almost immediately. One of the nice things about this process is that, upon compilation, your program will automatically have executable permissions, so there’s nothing to change in this regard.
Then, you can run the program with the command ./hello, which will print out Hello World! in your terminal. And that’s all there is to compiling C++ programs in Linux.
Here is the code for Hello World:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.