【问题标题】:How to activate CMakeSettings.json variables in VS2019如何在 VS2019 中激活 CMakeSettings.json 变量
【发布时间】:2020-11-09 19:49:34
【问题描述】:

我第一次在 VS2019 中尝试 CMake,但对它的工作原理感到困惑。 这是在 Windows 10 中使用 msvc_x64_x64 工具集。

我有一个创建静态库的 CMake 项目,我想使用动态链接。 默认的 CMakeSettings.json 包含变量

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

CMakeLists.txt 不会覆盖它,所以它对我来说很好。

但是,生成的 lib 文件似乎使用静态链接,另一个项目可以使用运行时库 /MTd 而不是 /MDd 链接到它。

所以它似乎是从其他地方获取编译器标志并忽略 CMakeSettings.json 中的那些,或者除了 CMAKE_CXX_FLAGS_DEBUG 之外还有一些变量。

如何让它使用 CMakeSettings.json 变量?

这是当前的 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": []
    }
  ]
}

【问题讨论】:

  • 你用的是什么生成器?
  • 我试过 Ninja 和微软的
  • 您介意发布您的整个 CmakeSettings.json 吗?应该不会那么多。
  • 感谢您的关注。 @hdf89shfdfs 文件在上面添加。

标签: visual-c++ cmake visual-studio-2019 microsoft-runtime-library


【解决方案1】:

找到了!

该库包含来自 GoogleTest 的文件,并且有一个文件 internal_utils.cmake 包含该行

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

所以 CMakeSettings.json 变量在使用之前就被覆盖了。

添加 CMake 命令参数

-Dgtest_force_shared_crt=ON

说服它停止。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2014-08-24
    • 2014-03-06
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多