MSVC_RUNTIME_LIBRARY defaults to dynamic linking for static libraries?!


I use CMake with CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to automatically generate both a shared dll and static .lib library on Windows when building with MSVC. I found out yesterday that the static .lib uses dynamic linking and hence requires using the /MD compiler option (instead of /MT). This is a bit surprising to me, why does it not use static linking for static .lib files?

In the CMake documentation ( I found:

MSVC_RUNTIME_LIBRARY: “If this property is not set then CMake uses the default value MultiThreaded$<$<CONFIG:Debug>:Debug>DLL to select a MSVC runtime library.”

What is the reason for using MultiThreadedDLL by default (instead of MultiThreaded) for static .lib files? Maybe that is a good choice and I just fail to understand…

Because you cannot mix them well. Compiling every static library with the static runtime is problematic when linking static and dynamic libraries into an executable.

OK, thanks that’s exactly what I wanted to know.

The .lib is not a static library here. It is an import library for the .dll library (which contains the actual runtime code).

Yes, I know. But in my project I have renamed that file to .dll.lib and I also generate a true static .lib library. And this static .lib is linked using /MD by default. (Which is totally fine, I just wanted to understand why /MD is used instead of /MT)

You can set the MSVC_RUNTIME_LIBRARY target property on the target to switch which variant of the runtime is used.

Yes thanks, based on the feedback from this thread I have added this as an option this morning: Add MSVC_CRT_STATIC option · kimwalisch/primesieve@a3a755f · GitHub