CMake in VSCode

VSCode is a free open source IDE with a lot of nice features. In addition one can chose from a variety of extensions. Looks like Cmake-tools kind of works, but the hotkeys and some settings are far from intuitive.

In my previous attempt I ended up removing cmake tools plugin and moving forward with custom task.json and launch.json scripts. Here is a template I made back then.

Recently I managed to make it more of less convenient (maybe some updates played a role here as well). Here are some settings I needed to make it usable and comparable to CLion.

Separate build directory per build type

If you have only one directory for your build you will need to rebuild everything when you switch between debug and release. In order to have a separate build directory per build type (Debug/Release) add the following to your settings.json:

"cmake.buildDirectory": "${workspaceRoot}/build-${buildType}"

By default VSCode and CmakeTools use ninja as a generator for cmake. Often one need to use make. Add

"cmake.generator": "Unix Makefiles"

to switch from ninja to make.

Debugging settings

In launch.json update "program" parameter:

"program": "${command:cmake.launchTargetPath}",

Cmake Tools will substitute the corresponding path. (from docs of CmakeTools)

Configure cmake settings

add cmake.configureSettings to settings.json:

"cmake.configureSettings": {
    "USE_MYMATH": "ON"

Getting started with Gtest

For any decent project in Cpp it's good to set up testing system. For C++ the standard solution is to use Gtest.

Installing Gtest in your ubuntu is not very straightforward. You have to install the package containing source code and then compile it with cmake with sudo rights.

sudo apt-get install libgtest-dev
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make

# copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib

Many projects use alternative approach. They clone GTest sources from github into the source tree of the project and compile it in-place. That requires some custom scripting in your cmake files.

Handling dependencies in CMake

If i need to import a compiled 3rdparty library to my cmake project:

add_library(some_library SHARED IMPORTED)

CMake with Conan and Catch2

Here is a sample CMake project that uses Conan for dependency management and Catch2 for testing:


Python bindings for C++ code

Hybrid Python/C++ packages, revisited. an approach using pybind11 and cmake.