How to Set Up C++ in Linux

Linux is a great C++ coding environment. Most Linux distros already have the essential components preinstalled, such as a compiler and the text editors are excellent for entering code into, including colour coding; and there’s tons of extra software available to help you out.

Step 1 – The first step is to ensure Linux is ready for your C++ code, so check the system and software are up to date. Open a Terminal and enter:

sudo apt-get update && sudo aptget upgrade

Then press Return and enter your password. These commands update the entire system and any installed software.

Step 2 – Most Linux distros come preinstalled with all the necessary components to start coding in C++; however, it’s always worth checking to see if everything is present. Still within the Terminal, enter:

sudo apt-get install build-essential

And press Return. If you have the right components nothing is installed; if you’re missing some then they are installed by the command.

Step 3 – Amazingly, that’s it. Everything is already for you to start coding. Here’s how to get your first C++ program up and running. In Linux Mint the main text editor is Xed, which you can launch by clicking on the Menu and typing Xed into the search bar. Click on the Text Editor button in the right-hand pane to open it.

Step 4 – In Xed, or any other text editor you may be using, enter the lines of code that make up your C++ Hello
World program:

#include <iostream>

int main()
{
//My first C++ program
std::cout << “Hello World!\n”;
}

Step 5 – When you’ve entered your code, click File > Save As and choose a folder in which to save your program. Name the file as helloworld.cpp (it can be any name as long as it has .cpp as the extension). Click Save to continue.

Step 6 – The first thing to notice is that Xed has automatically recognised this as a C++ file, since the file extension is now set to .cpp. The colour coding is present in the code and if you open up the file manager you can also see that file’s icon has C++ stamped on it.

Step 7 – With your code now saved, drop into the Terminal again. You need to navigate to the location of the C++ file you’ve just saved. Our example is in the Documents folder, so we can navigate to it by entering: cd Documents. Remember, the Linux Terminal is case sensitive, so any capitals must be entered correctly.

Step 8 – Before you can execute the C++ file you need to compile it. In Linux it’s common to use g++, an open source C++ compiler; as you’re now in the same folder as the C++ file, enter: g++ helloworld.cpp in the Terminal and press Return.

Step 9 – It takes a short time while the code is compiled by g++ but providing there are no mistakes or errors in the code you are returned to the command prompt. The compiling of the code has created a new file. If you enter: ls into the Terminal you can see that alongside your C++ file is a.out.

Step 10 – The a.out file is the compiled C++ code. To run the code enter: ./a.out and press Return. The words ‘Hello World!’ appear on the screen. However, a.out isn’t very friendly. To name it something else post-compiling, you can recompile with: g++ helloworld.cpp -o helloworld. This creates an output file called helloworld which can be run with: ./helloworld.

 

Find more guides like this in…

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button