<div dir="ltr">Yeah, that issue with /MD and /MT is the reason every project I've seen that deals with this problem uses something like this:<div><br></div><div>#<a href="https://cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F">https://cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F</a></div><div><div>foreach(flag_var</div><div>        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE</div><div>        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)</div><div>   if(${flag_var} MATCHES "/MD")</div><div>      string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")</div><div>   endif(${flag_var} MATCHES "/MD")</div><div>endforeach(flag_var)</div><div><br></div><div>Just appending also means that you cant just check if the compile flags contain one or the other using string(FIND...)</div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">/MD and /MT are by no means unique in being a compile flag that needs to be the same between linked packages. I'd forgotten about HAS_ITERATOR_DEBUGGING, but STL version also comes to mind (vs10 vs vs12, libstdc++ vs libc++, ect)</span></div> <br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 9, 2016 at 2:54 PM Daniel Schepler <<a href="mailto:dschepler@scalable-networks.com">dschepler@scalable-networks.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div>
<div style="direction:ltr;font-family:Tahoma;color:#000000;font-size:10pt">A while ago I did an experiment along the same lines, using something like this in CMake/MTflags.cmake.  (What we really needed was forcing /MT /D_HAS_ITERATOR_DEBUGGING=0 even
 on debug builds, because we were using an external library only available in /MT format.  So I might have accidentally broken it while adapting it here.)<br>
<br>
add_library(MTflags INTERFACE)<br>
target_compile_options(MTflags INTERFACE "/MT$<$<CONFIG:Debug>:d>")<br>
set_property(TARGET MTflags<br>
    PROPERTY INTERFACE_MSVC_MT ON)<br>
set_property(TARGET MTflags<br>
    APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL MSVC_MT)<br>
<br>
# Example usage:<br>
# include(MTflags)<br>
# add_library(A STATIC src1.cpp src2.cpp...)<br>
# target_link_libraries(A PUBLIC MTflags)<br>
<br>
That almost did what we wanted.  The biggest problem (and the one annoying enough to make me abandon the experiment for the time being) was: if you configured using the "NMake Makefiles" generator, then the compiler would give a warning on each source file
 because the build system was passing both the default /MD[d] and the additional /MT.<br>
-- <br>
Daniel Schepler<br>
<div style="font-family:Times New Roman;color:#000000;font-size:16px"></div></div></div><div><div style="direction:ltr;font-family:Tahoma;color:#000000;font-size:10pt"><div style="font-family:Times New Roman;color:#000000;font-size:16px">
<hr>
<div style="direction:ltr"><font color="#000000" size="2" face="Tahoma"><b>From:</b> CMake [<a href="mailto:cmake-bounces@cmake.org" target="_blank">cmake-bounces@cmake.org</a>] on behalf of Walter Gray [<a href="mailto:chrysalisx@gmail.com" target="_blank">chrysalisx@gmail.com</a>]<br>
<b>Sent:</b> Thursday, June 09, 2016 1:58 PM<br>
<b>To:</b> Cmake Mailing List<br>
<b>Subject:</b> [CMake] MSVC /M[TD]<d> as a compatible interface property?<br>
</font><br>
</div></div></div></div><div><div style="direction:ltr;font-family:Tahoma;color:#000000;font-size:10pt"><div style="font-family:Times New Roman;color:#000000;font-size:16px">
<div></div>
<div>
<div dir="ltr">
<div>Setting /MT or /MD is pretty important, and is a transitive requirement. See:
<a href="https://msdn.microsoft.com/en-us/library/2kzt1wy3(VS.80).aspx" target="_blank">
https://msdn.microsoft.com/en-us/library/2kzt1wy3(VS.80).aspx</a></div>
<div><br>
</div>
<div>The standard mechanism of using target_compile_options isn't quite appropriate because it will simply append the flags used by each library and use the last one with no complaint if there is a mismatch.</div>
<div><br>
</div>
<div>Example:</div>
<div>A uses /MT, B uses /MD, C Depends on both.</div>
<div>This will result in errors when compiling, but will configure just fine. I would like to make it so that if there is a mismatch between linked targets, cmake will either fail or issue a warning.</div>
<div><br>
</div>
<a href="https://cmake.org/cmake/help/v3.5/manual/cmake-buildsystem.7.html#compatible-interface-properties" target="_blank">https://cmake.org/cmake/help/v3.5/manual/cmake-buildsystem.7.html#compatible-interface-properties</a>
<div>This seems to be meant to be used for exactly this type of thing, but because there is no specific property for specifying MSVC_RUNTIME, that system cannot be used in this case.</div>
<div><br>
</div>
<div>Any thoughts on how to address this?<br>
<div><br>
</div>
</div>
</div>
</div>
</div></div></div></blockquote></div></div></div>