简体   繁体   English

如何在VS2019中激活CMakeSettings.json变量

[英]How to activate CMakeSettings.json variables in VS2019

I am trying CMake in VS2019 for the first time and am confused about how it works.我第一次在 VS2019 中尝试 CMake 并且对它的工作原理感到困惑。 This is in Windows 10 using the msvc_x64_x64 toolset.这是在使用 msvc_x64_x64 工具集的 Windows 10 中。

I have a CMake project that creates a static library, which I would like to use dynamic linking.我有一个 CMake 项目,它创建了一个 static 库,我想使用动态链接。 The default CMakeSettings.json includes variables默认的 CMakeSettings.json 包括变量

CMAKE_CXX_FLAGS_DEBUG = /MDd /Zi /Ob0 /Od /RTC1 CMAKE_CXX_FLAGS_DEBUG = /MDd /Zi /Ob0 /Od /RTC1

The CMakeLists.txt's do not override this, so it looks good to me. CMakeLists.txt 不会覆盖它,所以它看起来不错。

However, the lib file produced seems to use static linking and another project can link to it with runtime library /MTd not /MDd.但是,生成的 lib 文件似乎使用 static 链接,另一个项目可以使用运行时库 /MTd 而不是 /MDd 链接到它。

So it seems that it is getting the compiler flags from somewhere else and ignoring those in CMakeSettings.json, or there is some variable other than CMAKE_CXX_FLAGS_DEBUG.所以它似乎是从其他地方获取编译器标志并忽略 CMakeSettings.json 中的那些,或者除了 CMAKE_CXX_FLAGS_DEBUG 之外还有一些变量。

How can I get it to use the CMakeSettings.json variables?我怎样才能让它使用 CMakeSettings.json 变量?

Here is the current CMakeSettings.json file这是当前的 CMakeSettings.json 文件

{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Visual Studio 16 2019 Win64",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_CXX_FLAGS_DEBUG",
          "value": "/MDd /Zi /Ob0 /Od /RTC1",
          "type": "STRING"
        }
      ]
    },
    {
      "name": "x64-Release",
      "generator": "Ninja",
      "configurationType": "RelWithDebInfo",
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "variables": []
    }
  ]
}

Found it!找到了!

The library included files from GoogleTest and there was a file internal_utils.cmake that contained the line该库包含来自 GoogleTest 的文件,并且有一个文件 internal_utils.cmake 包含该行

        string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")

So the CMakeSettings.json variables were being overwritten before they could be used.所以 CMakeSettings.json 变量在使用之前就被覆盖了。

Adding a CMake command argument添加 CMake 命令参数

-Dgtest_force_shared_crt=ON

persuaded it to stop.说服它停止。

Thanks for your help.谢谢你的帮助。

Note that in VS2019, the correct way to do this is to use CMakePresets.json.请注意,在 VS2019 中,正确的做法是使用 CMakePresets.json。 CMakeSettings.json is deprecated. CMakeSettings.json 已弃用。 To enabled support for them, there is an option that needs to be enabled.要启用对它们的支持,需要启用一个选项。 See this link https://docs.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-160 for more information.有关详细信息,请参阅此链接https://docs.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-160

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM