240528 Visual Studio CMake Presets添加option

今天试了试用Visual Studio打开Qt的CMake项目,但是VS里用CMake的方式好像和Qt Creator的不一样。因为我的项目CMake里有一个自定义的option,在Qt的项目设置里有GUI可以设置那个option,但是在VS里找了一会没找到,而且VS好像是用cmake presets来设置的,于是问了一下Copilot。

简单来说就是在cacheVariables里加上要设置的option就行了,我这里是BUILD_EXAMPLE
之后还是得抽空学一学CMake。

以下是copilot的回答。
Certainly! To modify the CMakePresets.json file and set the "BUILD_EXAMPLE" option to true, follow these steps:

  1. Open the CMakePresets.json file in a text editor.

  2. Locate the appropriate configure preset where you want to set the "BUILD_EXAMPLE" option. In your provided example, we have two presets: "Qt-Debug" and "Qt-Release".

  3. Add or modify the "cacheVariables" section within the chosen configure preset. Specifically, add an entry for "BUILD_EXAMPLE" with the value "ON" (to set it to true). For example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {
    "name": "Qt-Debug",
    "inherits": "Qt-Default",
    "binaryDir": "${sourceDir}/out/build",
    "cacheVariables": {
    "CMAKE_BUILD_TYPE": "Debug",
    "CMAKE_CXX_FLAGS": "-DQT_QML_DEBUG",
    "BUILD_EXAMPLE": "ON" // Add this line
    },
    "environment": {
    "QML_DEBUG_ARGS": "-qmljsdebugger=file:{908eb81f-69af-44f1-b571-199332c0b484},block"
    }
    }
  4. Save the modified CMakePresets.json file.

Remember that CMakePresets.json specifies project-wide build details, so any changes you make here will affect the entire project. If you want to customize your local build details, consider using CMakeUserPresets.json instead⁴. Make sure to follow best practices for version control, such as ignoring CMakeUserPresets.json if you’re using Git.

源: 与 Copilot 的对话, 2024/5/28
(1) cmake-presets(7) — CMake 3.29.3 Documentation. https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html.
(2) CMake Presets integration in Visual Studio and Visual Studio Code. https://devblogs.microsoft.com/cppblog/cmake-presets-integration-in-visual-studio-and-visual-studio-code/.
(3) Build and Configure CMake Projects With CMakePresets. https://www.youtube.com/watch?v=vKUaWxemtWI.
(4) Simplified CMake Tutorial. https://www.youtube.com/watch?v=mKZ-i-UfGgQ.
(5) CMake presets | CLion Documentation - JetBrains. https://www.jetbrains.com/help/clion/cmake-presets.html.
(6) Configure and build with CMake Presets | Microsoft Learn. https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-170.