0

I am trying CMake in VS2019 for the first time and am confused about how it works. This is in Windows 10 using the msvc_x64_x64 toolset.

I have a CMake project that creates a static library, which I would like to use dynamic linking. The default CMakeSettings.json includes variables

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

The CMakeLists.txt's do not override this, so it looks good to me.

However, the lib file produced seems to use static linking and another project can link to it with runtime library /MTd not /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.

How can I get it to use the CMakeSettings.json variables?

Here is the current CMakeSettings.json file

{
  "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": []
    }
  ]
}
4

1 回答 1

0

找到了!

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

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

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

添加 CMake 命令参数

-Dgtest_force_shared_crt=ON

说服它停止。

谢谢你的帮助。

于 2020-11-10T18:12:52.407 回答