# How to set CMAKE\_MSVC\_RUNTIME\_LIBRARY for an external project?

**URL:** https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805
**Category:** Usage
**Created:** [February 20, 2021, 7:56pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805 "2021-02-20T19:56:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![TomzBench](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomzbench/32/1205_2.png) [@TomzBench](https://discourse.cmake.org/u/TomzBench)
#### Post date: [February 20, 2021, 7:56pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/1 "2021-02-20T19:56:00Z")

</div>

When building my project for Windows - I notice the external project does not seem to honor my root project MSVC\_RUNTIME\_LIBRARY flag. I tested this by modifying my external project CMAKELISTS and the results were more as expected. However, is there a way to set this flag with out modifying my external project source code? Is there a way to pass this variable to the external project? Or should I use CMAKE\_C\_FLAGS and CMAKE\_CXX\_FLAGS?

---

<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: [February 21, 2021, 5:00pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/2 "2021-02-21T17:00:24Z")

</div>

These kinds of settings are usually meant to be under some control by the end user building the project in question. I think a better solution would be to add the property name to the [`COMPATIBLE_INTERFACE_STRING`](https://cmake.org/cmake/help/latest/prop_tgt/COMPATIBLE_INTERFACE_STRING.html) property. CMake can at least warn when a bad setting is combined on a per-target basis rather than a project-wide basis (e.g., a project wanting one part to work with your project and another which doesn’t care that wants to use a different setting for whatever reason).

---

<div class="post-metadata">

### Author: ![TomzBench](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomzbench/32/1205_2.png) [@TomzBench](https://discourse.cmake.org/u/TomzBench)
#### Post date: [February 22, 2021, 3:16pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/3 "2021-02-22T15:16:04Z")

</div>

In this case “I” am the end user, and my external project I am dependent of seems to not honor my wish of MSVC runtime linkage. Is this a problem with my external project’s CMakeLists file? IE they are overwriting the CFLAGS and hard coding the runtime?

This is a third party CMakeLists file not under my control. I could fork and fix if this is the case - their CMakeLists file is pretty old looking and hard for me to reason about.

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [February 22, 2021, 6:33pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/4 "2021-02-22T18:33:01Z")

</div>

How are you setting CMAKE\_MSVC\_RUNTIME\_LIBRARY? I believe you must set this variable before any targets are defined to get the behavior you desire.

How are you consuming your external project? add\_subdirectory?

---

<div class="post-metadata">

### Author: ![TomzBench](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomzbench/32/1205_2.png) [@TomzBench](https://discourse.cmake.org/u/TomzBench)
#### Post date: [February 22, 2021, 6:34pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/5 "2021-02-22T18:34:45Z")

</div>

I set CMAKE\_MSVC\_RUNTIME\_LIBRARY in my root CMakeLists file near the top of my project before including any other libraries or projects. I add my external project by use ExternalProject\_Add(…)

---

<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: [February 22, 2021, 8:18pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/6 "2021-02-22T20:18:49Z")

</div>

Oh, such variable settings don’t propagate across `ExternalProject_Add`. Try passing `-DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}` in `CMAKE_ARGS` for that project?

---

<div class="post-metadata">

### Author: ![TomzBench](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomzbench/32/1205_2.png) [@TomzBench](https://discourse.cmake.org/u/TomzBench)
#### Post date: [February 28, 2021, 10:32pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/7 "2021-02-28T22:32:33Z")

</div>

@ben.boeckel I attempted that - but it did not seem to work. I am guessing it might be related to the cmake Policies? I think this is a new feature that needs to be opted into by the cmake project. External projects perhaps need to enable the same policies seperately? The external project i am linking to has some pretty old looking cmake. I’m guessing this could be the problem.

Passing in `-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} /MT"` directly doesn’t work either. I am thinking it is a problem with the external project’s CMakeLists. (zeromq)

---

<div class="post-metadata">

### Author: ![TomzBench](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomzbench/32/1205_2.png) [@TomzBench](https://discourse.cmake.org/u/TomzBench)
#### Post date: [March 1, 2021, 3:16pm UTC](https://discourse.cmake.org/t/how-to-set-cmake-msvc-runtime-library-for-an-external-project/2805/8 "2021-03-01T15:16:37Z")

</div>

I got this solution to work by passing in the CFLAGS directly to the main project. It’s not clear to me if I can pass in the CMAKE\_MSVC\_RUNTIME\_LIBRARY option if the external project policies are not enabled for this feature. So in order to support an older cmake project i believe i have to pass in the CFLAGS directly…
