1

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": []
    }
  ]
}
user1683586
  • 373
  • 2
  • 12

2 Answers2

0

Found it!

The library included files from GoogleTest and there was a file internal_utils.cmake that contained the line

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

So the CMakeSettings.json variables were being overwritten before they could be used.

Adding a CMake command argument

-Dgtest_force_shared_crt=ON

persuaded it to stop.

Thanks for your help.

user1683586
  • 373
  • 2
  • 12
0

Note that in VS2019, the correct way to do this is to use CMakePresets.json. CMakeSettings.json is deprecated. To enabled support for them, there is an option that needs to be enabled. See this link https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-160 for more information.

markf78
  • 597
  • 2
  • 7
  • 25