MSVC_RUNTIME_LIBRARY completely ignored

Hello,

i have a weird problem with CMake and the MSVC runtime on windows.

My application is statically linked against the msvc runtime, which means all libraries i am linking statically, also have to be linked against the msvc runtime statically.
This works as expected for most of my libraries.

However i have a problem with libdatachannel. Libdatachannel provides a CMake project, with which i can generate nmake files. They provide a target “datachannel-static” with which i can generate a static library. The problem is that this static library is linked against the msvc runtime dynamically.
With the help of the maintainers of the libdatachannel project, i tried the following command to force the linking of the static runtime:

cmake -B ../libDataChannel-build -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENSSL_ROOT_DIR=<..> -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded"

This gets completely ignored and the generated makefiles contain the /MD directive in the CXX flags.
I then tried setting the variable directly in the libdatachannel CMakeLists.txt with:

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")

This also is ignored. I then tried to set the property for every target of libdatachannel:

set_target_properties(datachannel-static PROPERTIES
  MSVC_RUNTIME_LIBRARY "MultiThreaded"
)

Also without any success.
Only forcing the CXX flags and manually replacing /MD with /MT works.

It is noteworthy that on one windows machine, with the same setup as on the other, setting the MSVC runtime via command line is working, while on the other machine it is not. Both use cmake version 3.26.4 and are executed in the Visual Studio 2022 Developer Command Prompt v17.8.6.
This also applies to the sub-projects that are used by libdatachannel and utilize cmake.

I dont know what could be the cause of this issue.

Thanks in advance

Is there a minimum CMake version specified? It looks like it needs to be at least 3.15 for those properties to actually matter. Alternatively you can set policy CMP0091 to NEW.

1 Like

You are completely right. I missed that.
I added the following to the cmake configuration arguments for libdatachannel:
-DCMAKE_POLICY_DEFAULT_CMP0091=NEW

It is now building correctly, thanks