[CMake] MSVC /M[TD]<d> as a compatible interface property?

Daniel Schepler dschepler at scalable-networks.com
Thu Jun 9 17:54:45 EDT 2016


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

add_library(MTflags INTERFACE)
target_compile_options(MTflags INTERFACE "/MT$<$<CONFIG:Debug>:d>")
set_property(TARGET MTflags
    PROPERTY INTERFACE_MSVC_MT ON)
set_property(TARGET MTflags
    APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL MSVC_MT)

# Example usage:
# include(MTflags)
# add_library(A STATIC src1.cpp src2.cpp...)
# target_link_libraries(A PUBLIC MTflags)

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.
--
Daniel Schepler
________________________________
From: CMake [cmake-bounces at cmake.org] on behalf of Walter Gray [chrysalisx at gmail.com]
Sent: Thursday, June 09, 2016 1:58 PM
To: Cmake Mailing List
Subject: [CMake] MSVC /M[TD]<d> as a compatible interface property?

Setting /MT or /MD is pretty important, and is a transitive requirement. See: https://msdn.microsoft.com/en-us/library/2kzt1wy3(VS.80).aspx

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.

Example:
A uses /MT, B uses /MD, C Depends on both.
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.

https://cmake.org/cmake/help/v3.5/manual/cmake-buildsystem.7.html#compatible-interface-properties
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.

Any thoughts on how to address this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160609/29d1a802/attachment.html>


More information about the CMake mailing list