LOADING

Wait For a Moment...

CMake Tools for VS Code Documentation

2023/2/2 CMake

CMake is an open-source, cross-platform tool that uses complier and platform independent configuration files to generate native build tool files specific to your complier and platform. And the CMake tools extension integrates VS Code and CMake to make it easier to configure, build and debug your C++ project.

CMake Tools for VS Code Documentation

Get started with CMake Tools on Linux

CMake is an open-source, cross-platform tool that uses complier and platform independent configuration files to generate native build tool files specific to your complier and platform. And the CMake tools extension integrates VS Code and CMake to make it easier to configure, build and debug your C++ project.

  1. Check your environment.

Although you’ll use VS Code to edit your source code, you’ll compile, debug the source code using complier and debugger, and build tools installed in your system. Check your environment to ensure your GCC, GDB and CMake installed, as well as CMake Tools on VS Code.

  1. Create CMakeLists.txt

CmakeLists.txt would tell the CMake tools how to builds your project, which contains a set of directives and instructions describing the project’s source files and targets (executable, library or both).

  1. Select a kit

Before you can access to use CMake Tools to organize your project, you have to configure the extension know about the compliers on your system. A kit represents a toolchain, which is the compiler, linker and other tools used to build your project.

  1. Select a variant

A variant contains instructions for how to build your project. By default, the CMake Tools extension provides four variants, each corresponding to a default build type: Debug, Release, MinRelSize, and RelWithDebInfor. These options do the following:

Debug: disables optimizations and includes debug information. Release: Includes optimizations but no debug information. MinRelSize: Optimizes for size. No debug information. RelWithDebInfo: Optimizes for speed and includes debug info.

  1. CMake: Configure & Build

Run the CMake: Configure command to configure your project. This generates build files in the project’s build folder using the kit and variant you selected. After configuring your project, then just click the Build button of the status bar at the right-below corner.

The CMake configure proces

In CMake, Configure refers to detecting requirements and generating the build files that will produce the final complicated artifacts. The following concepts will help you to understand how CMake Tools interacts with Cmake’s confugure process.

  • The CMake Cache is a list of key-value pairs that persist between runs of the configure process.
  • Cache initializer arguments are the arguments passed to CMake that set values in the cache before any CMake scripts are run. These allow you to control the build settings.
  • Unless overwritten or deleted, values in the CMake Cache persist between CMake runs.
  • CMake doesn’t do the build itself, it relies on build tools installed on your system. The result of a configure depends on the CMake Generator. The Generator tells CMake what kind of tool will be used to compile and generate the results of the build. Here are several families of generators availabel.
Generator Description
Ninja Emits files for Ninja Build Tools. This is the default generator that CMake Tools uses, unless configured otherwise.
Makefile Emits a Makefile for the project that can be built via make
Visual Studio Emits visual studio solutions and project files. There are many different Visual Studio generators, so it is recommended to let CMake Tools automatically determine the appropriate generator.

The Cmake Tools configure step

  1. The activate kit. CMake kits provide information about toolchains available on your system that can be used with CMake to build your project.
  2. Which generator to use. CMake doesn’t deal the build process itself, instead it requires specific generator (the default option is Nanja on VS Code) to do the work.
  3. The configuration options. CMake Tools has a variety of locations where configuration options can be define.
  4. The configuration environment. CMake Tools sets environment variables for the child process it runs for CMake.

Build with CMake Tools

Once you have configured your project, then you can start to run a CMake build. Most of your time with CMake Tools will be spend in the process of configuring the build. You can just click the build button in the VS Code status bar. Starting a new build while an existing build is running will cancel the current build and start a new one.

Build the target

CMake Tools persists a “default target” for the build process. The default target is the “all” target, which builds all of the targets that CMake has designated for a default build.

Default target as shown in the status bar

Also, you can build a single target without changing the current build target from the VS Code by running the CMake: Build a target command. CMake will build any dependent targets, even if they aren’t directly selected.