8

I recently started using VS Code and I have a question. How do I set the values of the compilation options defined in CMakeList.txt? For example if the following option is set in the CMakeList.txt: option(BUILD_WITH_TESTS "Build with tests." OFF) Then I can set this value when building using for example the CMake GUI: selecting option value

Does a similar toolkit exist in VS Code? What are the ways to set value BUILD_WITH_TESTS when building a project in VS Code?

At the moment I'm using VS Code with the plugin CMake Tools 1.5.3. But I do not find such functionality as a CMake GUI there.

1
  • And how to perform a building with parameters in VS Code without using a terminal? For example, a building equivalent to the following command: cmake -BUILD_WITH_TESTS=ON Commented Jan 26, 2021 at 2:15

1 Answer 1

6

You have several choices, depending on your exact requirements.

The CMake Tools extensions is a good choice, but it doesn't come with a GUI. If you want to use cmake-gui, you could still use it in your vscode CMake build directory, by executing cmake-gui <path_to_build_folder> in the vscode terminal.

In case you need to run this command often, you could customize your vscode settings, for a better cmake-gui integration, by using the Command Runner extension, which allows to run custom shell commands. After installation add the following to your settings.json:

  "command-runner.commands": {
    "cmake-gui": "cmake-gui ${workspaceFolder}/build"
  }

and, if you like, a key binding to keybindings.json

  {
    "key": "ctrl+alt+1",
    "command": "command-runner.run",
    "args": { "command": "cmake-gui" }
  }

Personally I would do none of the above methods, but just use ccmake in the terminal.


All of the above describes how to change settings in the current existing build directory (which is usually created by CMake Tools on first launch). However, the settings will get lost if you switch to a different compiler or delete the CMake cache in any other way.

To persist your settings and configure CMake by default with your preferred configuration, you can add your CMake arguments to your vscode workspace settings (in ${workspaceFolder}/.vscode/settings.json), e.g.

{
    "cmake.configureArgs": [
        "-DBUILD_WITH_TESTS=ON"
    ]
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your quick response! When I add to settings { "cmake.buildArgs": [ "-DBUILD_WITH_TESTS=ON" ] } A build error appears: invalid option -- 'D'
[proc] Executing command: /usr/bin/cmake --build /home/Projects/proj/build --config Debug --target proj -DBUILD_WITH_TESTS=ON -- -j 4 [build] /usr/bin/make: invalid option -- 'D'
Sorry, it is cmake.configureArgs. I update the answer.
How do I run cmake in the terminal? I have the extension installed, but the terminal says command not found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.