Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

Setting CMake compilation options in VS Code

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.

question from:https://stackoverflow.com/questions/65894922/setting-cmake-compilation-options-in-vs-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
    ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...