How to set CMAKE_MSVC_RUNTIME_LIBRARY for an external project?

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?

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 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).

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.

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?

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(…)

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?

@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)

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…