# MSVC\_RUNTIME\_LIBRARY defaults to dynamic linking for static libraries?!

**URL:** https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084
**Category:** Code
**Tags:** comp:msvc, gen:vs, os:windows
**Created:** [September 14, 2021, 7:07am UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084 "2021-09-14T07:07:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [September 14, 2021, 7:07am UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/1 "2021-09-14T07:07:49Z")

</div>

Hi,

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 ([https://cmake.org/cmake/help/latest/prop\_tgt/MSVC\_RUNTIME\_LIBRARY.html](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html)) 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…

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [September 14, 2021, 8:28am UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/2 "2021-09-14T08:28:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [September 14, 2021, 8:39am UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/3 "2021-09-14T08:39:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 14, 2021, 12:51pm UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/4 "2021-09-14T12:51:02Z")

</div>

> [@walki](#):
>
> 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

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

---

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [September 14, 2021, 2:05pm UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/5 "2021-09-14T14:05:03Z")

</div>

> [@ben.boeckel](#):
>
> 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`)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 14, 2021, 2:05pm UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/6 "2021-09-14T14:05:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [September 14, 2021, 2:08pm UTC](https://discourse.cmake.org/t/msvc-runtime-library-defaults-to-dynamic-linking-for-static-libraries/4084/7 "2021-09-14T14:08:02Z")

</div>

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](https://github.com/kimwalisch/primesieve/commit/a3a755f6442ab040c79bd2ba994bec1cbe7d0c85)
