Writing and Compiling C++ on Linux [A How-To Guide]

If you’ve adopted Linux, chances are you might have done so for development purposes. After all, it has everything you need to program in most languages, and do so for (almost) free.

With Linux you can program in some of the most important languages on the planet, such as C++. In fact, with most distributions, there’s very little you have to do to start working on your first program. And what’s better, you can easily write and compile all from the command line. 

If you’re a lone programmer or you work for a custom software development company like BairesDev, it should take you no time to get up to speed on programming with Linux as your platform of choice. 

With that said, I want to guide you through the process of writing and compiling your first C++ program on Linux. I’ll demonstrate how this is done on both Ubuntu and Red Hat distributions.

What you’ll need

The only things you’ll need for this tutorial are:

A running instance of a Ubuntu- or Red Hat-based Linux distribution.

Some C++ code.

And that’s it. 

I’ll be demonstrating with the tried and true “Hello, World!”. This is an incredibly basic example, but it makes it easy for new users to follow. If you’re unfamiliar with it, all it does is print out the phrase “Hello, World!” on the screen.

Installing the necessary tools

Although there are some Linux distributions that come with everything you need to start developing (out of the box), you may come across one that doesn’t. Without the right tools, your bespoke software development experience will get frustrating fast. 

So, how do you install the necessary software? Let’s do this on Ubuntu first. Open a terminal window on your desktop and issue the command:

sudo apt-get install build-essential -y

In order to do this on Red Hat, you’ll use the dnf command with the groups options like so:

sudo dnf group install “Development Tools”

Either command will install everything you need to compile your first C++ application.

Writing the program

Now we need to write the “Hello, World!” program. Because this is a simple application, you can use the Nano editor. Open a terminal window and issue the command:

nano hello.cpp

That command will create a new file, named hello.cpp, and open it for editing. In that empty file, paste the following text:

#include <iostream>
using namespace std;
int main() 
{
    cout << "Hello, World!" n;
    return 0;
}

Save and close the file with the keyboard shortcut [Ctrl]+[X] and then type “y” (no quotes) to use the name we gave the file from the start.

At this point you now have your C++ file, hello.cpp, ready to compile.

Compiling the program

The next step is to compile our newly-written program. The command to do this is really quite simple. The basic command is:

g++ hello.cpp

That command will compile the program and create an executable file named a.out. Not very helpful, right? So instead of letting g++ name the executable, let’s give it the name hello, by using the output option (-o) with the command:

g++ -o hello hello.cpp

The above command will compile the hello.cpp file and create a new executable binary, named hello.

Running the new program

Now that you’ve used g++ to compile your program, it’s time to run it. Because this is a terminal-only application, you have to run it from within the terminal as a command. To do this, issue the command:

When you run the above command, you should see the output of the Hello, World! program (Figure 1).

(Figure 1)

The Hello, World! program output.

The reason why you have to run the program with the leading ./ characters, is because the program isn’t in your $PATH, which is a collection of directories wherein a command can be run globally. Because of this, you have to run the command from within the directory housing the hello binary.

Let’s say, however, you want to be able to run that Hello, World! application from within any directory on your Linux machine. If you want to do that, you must copy the binary file into a directory within your $PATH. To find out what directories are in your $PATH, issue the command:

echo $PATH

This will list out every directory in your $PATH (Figure 2).

Figure 2

All of the directories in a user $PATH.

A safe bet is always /usr/local/bin. Copy that binary file with the command:

sudo cp hello /usr/local/bin

Now, all you have to do is issue the command hello to see the output of the Hello, World! program.

Conclusion

Writing and compiling your first program on Linux wasn’t nearly as hard as you assumed it would be, right?. From this very basic example, you can start programming and compiling more and more complex applications, until coding on Linux is second nature.

read original article here