So, you want to program in C++ on Linux? Here's how to set up your "dev. environment" quickly. It's mostly easy, with one potential pitfall...

GCC, CMake & VS Code

Here's what we're going to install:

  • GCC - the C/C++ compiler that's included with Linux
  • CMake - the de-facto build system for C++ (you'll find it almost everywhere)
  • Visual Studio Code (a.k.a., VS Code) - a lightweight and (relatively) easy to use code editor. Actually, it's much more than just an editor. It's an Integrated Development Environment (IDE), which is a fancy way of saying that you can compile and debug code right from.

Installing on Debian, Ubuntu, Mint (and Related)

How to install it depends on your linux distro. If you're using a Debian based distro like Ubuntu or Mint, then head over to VS Code's download page (link), and download the *.deb file.

VSCode Download DebianNow install VS Code by either double-clicking on the downloaded .deb file, or enter the following from a terminal:

sudo apt install <vs-code's filename>.deb

Replace <vs-code's filename>.deb with the name of the file you downloaded.

NOTE: This will automatically set up Microsoft's package repositories so you can easily keep VS Code up-to-date.

Installing GCC and cmake is very easy. Enter the following in a terminal window:

sudo apt-get install build-essential gdb cmake

Installing on RedHat, Fedora, OpenSUSE, etc.

Installing VS Code on RedHat based Linux distros is a slightly more manual process. Forget about downloading the *.rpm file. Instead, open a terminal window, and add Microsoft's package repository to your system using the following:

sudo rpm --import
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=\nenabled=1\ngpgcheck=1\ngpgkey=" > /etc/yum.repos.d/vscode.repo'

Now update the package manager and install code. With Fedora 22 and newer, use dnf:

dnf check-update
sudo dnf install code gcc gcc-c++ kernel-devel make gdb cmake

Or, use yum on older systems:

yum check-update
sudo yum install code gcc gcc-c++ kernel-devel make gdb cmake

If you look at the scripts above, you'll notice that we've also installed GCC's C & C++ compilers, the GDB debugger, and cmake in one go.

Integrating CMake and the Compiler into VS Code

We're almost done. All that remains is to install C/C++ and cmake extensions into VS Code. So, start VS Code. You can do this either from the desktop menus, or enter "code" into a terminal window (minus quotation marks), and hit enter.

On VS Code's left side you'll find a set of icons. Click the "Extensions" icon (see below).

VSCode ExtensionsNext, enter "C/C++ Extension Pack" into the extensions search box (minus the quotation marks). The first extension in the list should be "pass:[C/C++] Extension Pack" by Microsoft. Click the blue "Install" button, and it'll install all the extensions we need for both C/C++ and CMake.

Congratulations! You're now ready to create your first CMake project...

Your First C++ Project

I've created a simple project for you to try out. Click here to download it. Unzip it to a directory of your choice. Next, open the newly created sub-directory (called hello_cmake) in VS Code.

You'll see the CMake toolbar down the bottom of the VS Code window:

CMake Toolbar

Click the run button (the triangular button). All going well, VS Code will build and run the code, and you should see "Hello CMake!" printed in VS Code's terminal (which should be right above the CMake toolbar.

The Pitfall - Old CMake Versions

Some Linux distros are rather "conservative" with updating packages. So, you may end up with CMake (and other software) being several versions behind the latest. Not good if you want to use recently added features and/or bug-fixes.

If this happens, then building your C++ project will fail with an error such as:

CMake error at CMakeLists.txt:1 (cmake_minimum_required)
    CMake 3.28 or higher is required. You are running version 3.10.2

Here are a few solutions:

  • Update to a newer (or different) Linux distro
  • Install the snap package version of CMake (it tends to be very recent)
  • Build CMake from its source code

Hopefully you won't need to do this, though.

Learn CMake Without the Headache

This video & blog post came from The CMake Tutorial.

CMake is a powerful tool, but it can be hard to learn how to use it. The CMake Tutorial can save you loads of time, and take the frustration and pain out of learning how to use CMake.

Click here to get The CMake Tutorial.

The CMake Tutorial