Compiling C++ on Linux with VS Code
- The Founder

- Oct 12
- 9 min read
Today, we will be exploring how to write, build and run our own C++ programs in Linux.
Introduction
In order to create and test C++ programs, three vital tools are needed.
Compiler
Firstly, we need a compiler. A compiler takes your human-readable C++ code and converts it (compiles it) into the computer-readable machine language that the computer actually understands. For desktop platforms, this typically results in an executable file that the user can launch. Fortunately, unlike many other platforms, Linux Mint should already have G++, a very popular C++ compiler within the GNU Compiler Collection, commonly found pre-installed for many Linux applications.
Debugger
Creating C++ programs is not always about success. More often or not, there are parts of your program that are not working, won't compile or are not acting the way you expected them to. To overcome this, a debugger can be used to investigate issues with your code so you can apply a fix. Debugging is far more involved than compiling, so it will be the subject of its own in-depth tutorial.
IDE
Once we have an idea of the program we want to create, and access to the tools to compile it, we now need something to write it with. In many cases, you can use a simple text editor to write the code itself, then pass this file to the compiler. However, this can become inconvenient when working on large programs, as it will not include all of the special tools and other benefits provided by an Integrated Development Environment (IDE).
For this tutorial, I will be using Microsoft VS Code, as it is a very popular multi-language IDE, used by many beginners and professionals in the industry.
Compiler and Debugger
To check if your Linux distribution comes with the G++ compiler pre-installed, run the following command in a terminal console:
g++ -vThis command will check if the compiler is installed and display its version on the screen.

We can perform the same test for the GDB debugger.
gdb -vThis command should display the detected version, similar to what is depicted below:

Installing Missing Compiler/Debugger
In some cases, these tools may not be installed. This can be easily overcome by entering the following line of instructions.
sudo apt-get updatesudo apt-get install build-essential gdbThe first line will update your system's software package manager, ensuring we get the latest version of the tools we wish to install. The second line then installs the missing packages onto your system. These instructions can be found the the VS Code installation on Linux page: https://code.visualstudio.com/docs/cpp/config-linux
Installing VS Code
VS Code is a simple-to-use, free, and versatile IDE (Integrated Development Environment). This tool, produced by Microsoft, allows you to create projects from a range of supported languages, with plenty of useful add-on extensions available to level up your coding experience.
For many of the tutorials on this site, VS Code will largely be used as a one-size-fits-all tool for the various programming languages we will explore.
Get the Download Link
To get started, go to the site: https://code.visualstudio.com/download
This page provides a range of download options for the Windows, Mac OS, and Linux platforms.

My personal go-to Linux is Linux Mint. This is a primarily Debian/Ubuntu-based distribution, so I will be downloading the .deb installable package. However, you can choose whatever option best suits your specific platform. Similarly, if you are using Raspberry Pi OS, this is also a suitable option.
The ".deb" file is typically around 100MB in size, so it should not take too much more than a minute to download on relatively slow internet.
Installing the Package
As I downloaded the .deb format, I don't need to use any fancy terminal commands. To install the package, all you need to do is find the file in your downloads folder, right-click, and select the option: "Open With Package Installer".

After selecting this option, a window as shown below should be displayed:

Now, to finish, just simply select "Install Package". If you have a password on your Desktop account, this may have to be entered to proceed.

You may also be prompted to add the Microsoft apt repository. Enabling this will allow you to update VS Code through your Linux system's package manager.

Launchable Icon
In the system menu, VS Code should now appear as a launchable icon. In my case, a new Development tap was produced. If you have other coding programs already installed, VS Code will likely show in the same existing tab.

Installing C++ Extensions
With VS Code installed, we can now add the C++ extensions, providing powerful tools that can enhance our programming capability.
Searching and installing these tools can be done through the extensions marketplace window, accessible by the icon on the sidebar.

The tools we need today are the Microsoft C/C++ and C/C++ Extension packages. Another popular tool is the C/C++ Runner tool, but we won't be using it in this tutorial.

These packages will provide VS Code with the foundational tools for editing C++ code. This also includes IntelliSense, which acts like a suped-up spell check, offering assistance in correcting code grammar, auto-fill suggestions and displaying information on hovered-over commands.
Click on the small blue install button, or click on the extension itself to read up more about what they offer.
Advertisements provided by Google AdSense
Creating a Test Program
With our system now ready with the essential tools for C++ development, we can put it to the test with a simple program.
Creating a Test File
First, we need a place to store our code. Ideally, you need a dedicated folder per project, as this will contain all supplementary files generated by your system, rather than being spread and mixed with your other desktop files.
We can make our working directory by right-clicking and creating a folder in your desired location like normal, then opening this folder within VS Code.

Alternatively, this directory can be created and opened within the command line, and then VS Code can be launched from this location. This can be useful if other command-line instructions need to be run within this location later.
cd Desktop
mkdir TestCCP
cd TestCPP
code .This code allows us to change directory (cd) to a desired location, as well as make directories (mkdir) with custom names. The command "code ." will launch VS Code with the current directory set as the "workspace".
We should be able to see our directory opened with the explorer, with the option to now create a new file. We will use this to create a file with the ".cpp" extension, which signifies this is a C++ code file.

Once a filename is chosen, a new editor tab should open, which is where we will write and edit our code.

The Code
To create a simple test program that displays the message "Hello World" on the screen, enter and save the following code:
#include <iostream>
using nsamespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
} Without going into detail, this code means:
// Include the package needed for displaying messages on the terminal.
#include <iostreram>// Include the commands from the "C++ standard library" namespace
using namespace std;// The "main" function, called by the computer to start the program.
int main()
{
}// Send out to the console the following stream of data, then end the line.
cout << "Hello World!" << endl;// Tell the computer that the program has reached the end succesfully (No Errors Occured)
return 0;Running our Program
There are two main ways of running our program. The first is through the triangular "Run C/C++ File" button within VS Code, which will automatically compile and run the program. The other option is to separately compile and then run the program using the command terminal.
Option 1: Within VS Code
To run the program within VS Code, first ensure the file is saved, then select the "Run C/C++ File" option in the top right.

Before proceeding, unless otherwise specified, VS Code will ask which compiler you want to use. As we intend to work more on the side of C++ over just C, we can select the second option, to specify the G++ compiler. This choice will be saved within your project's configuration files after the first time it has been run.

Once the program has been compiled and run, we can see some information displayed within the tabs along the bottom of VS Code. In this scenario, the Debug tab displays a large amount of information, most of which does not concern us at this stage.

However, if we take a look at the very end, we see the message that reads "The program '...' has exited with code 0 ...". This tells us the file has run successfully with a zero exit code, indicating no error, where another number will signify the type of error that occurred.
More importantly, if we take a look at the terminal, we see our "Hello World" message.

This is the console our code is streaming data out to, demonstrating that we can successfully run basic console programs.
Option 2: Manually From the Terminal
The more manual, and my preferred method, of building and running C++ programs is through the terminal console. I find this gives us a clean and structured way of building our code, whilst additionally building our command-line skillset.
First, we need to open a terminal and enter the directory where our code is located.

The project directory should be selected by default if you open a new bash terminal within VS Code.

To compile the program, we need to call the g++ compiler, set the output filename, and specify the C++ source file we wish to compile.

The chosen compiler:
g++ -o TestProg TestProg.cppDesignating the intended filename for the output file:
g++ -o TestProg TestProg.cppSpecifying the source file we wish to compile:
g++ -o TestProg TestProg.cppUsing the "dir" command to list the contents within the directory, we should now see the executable file labelled with the filename we provided. This is one benefit of using the command-line approach, as we can specify the name for the output, allowing us to keep older versions of our programs. For example, comparing speeds or memory utilisation between versions.

To run the program, we just need to use the following command, specifying the name of the file and its location in the file path.

After running the program, we can see the message "Hello World" displayed on the screen.

Though this is a more manual way of performing this process, we can see that it only takes a few lines to fulfil, taking up minimal space on the terminal.
We can also perform this operation with VS Code, to keep our file creation, file editing, compilation and file execution all in one place. In this case, I have designated the output file to be called "TestProg2" to differentiate it from the previous attempt.

Personally, I prefer using the external terminal, as it allows me to separately partition my editing and executing processes. Doing this allows me to use the full editor to maximise the code I can view and enlarge the amount of data visible in the terminal window. Using VS Code leads to rearranging and resizing windows, whereas through the terminal, I can more easily switch between programs using the taskbar and use as much space for each as I desire, including different displays.
Advertisements provided by Google AdSense
For a more in-depth insight into this topic, check out the related posts:
RELATED POSTS
C++ Begugging in VS Code (Coming Soon)

