Get started with CMake Tools on Linux
CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform.
The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.
In this tutorial, you'll use the CMake Tools extension for Visual Studio Code to configure, build, and debug a simple C++ CMake project on Linux. Aside from installing CMake, your compiler, debugger, and build tools, the steps in this tutorial apply generally to how you'd use CMake on other platforms, like Windows.
If you have any trouble, please file an issue for this tutorial in the VS Code documentation repository. Also, for more information about CMake Tools in general, see CMake Tools for Visual Studio Code documentation
Prerequisites
To complete this tutorial on Ubuntu, install the following:
-
C++ extension for VS Code. Install the C/C++ extension by searching for 'c++' in the Extensions view (
Ctrl+Shift+X
). -
CMake Tools extension for VS Code. Install the CMake Tools extension by searching for 'CMake tools' in the Extensions view (
Ctrl+Shift+X
). -
You'll also need to install CMake, a compiler, a debugger, and build tools.
Video: What is a build system? How do you add CMake to your project?
Watch this video to understand when a build system will help you and how to set up CMake for your project, or follow the steps in the following sections.
Ensure that CMake is installed
The VS Code CMake Tools extension does its work by using CMake installed on your system. For best results, use CMake version 3.27 or greater.
See if CMake is already installed on your system. Open a Terminal window and enter the following command:
cmake --version
To install CMake, or to get a later version if you don't at least have version 3.27, see the instructions for your platform at Kitware APT Repository. Install version 3.27 or greater.
Ensure that development tools are installed
Although you'll use VS Code to edit your source code, you'll compile and debug the source code using the compiler, debugger, and build tools (such as make
) installed on your system.
For this tutorial on Ubuntu, we'll use the GCC compiler, GDB to debug, and make
to build the project. These tools are not installed by default on Ubuntu, so you need to install them. Fortunately, that's easy.
Check if GCC is installed
To see if GCC is already installed on your system, open a Terminal window and enter the following command:
gcc -v
If GCC isn't installed, run the following command from the Terminal window to update the Ubuntu package lists. An out-of-date Linux distribution can interfere with getting the latest packages.
sudo apt-get update
Next, install the GNU compiler, make
, and the GDB debugger with this command:
sudo apt-get install build-essential gdb
Create a CMake project
If you do not have an existing CMake project, follow the steps in Create a CMake project.
If you already have an existing CMake project that has a CMakeLists.txt
file in the root directory, continue to Configure Hello World to configure your project.
Configure Hello World
Before you can use the CMake Tools extension to build a project, you need to configure it to know about the compilers on your system. There are two ways to configure CMake in VS Code:
- Use CMake Presets (recommended)
- Use CMake Kits/Variants