I've got a CMakeLists where I want to build some targets using the dynamic version of the C runtime, and some other targets using the static version.
Because this needs to be set per target, the default method of setting CMAKE_CXX_FLAGS_<Config>
does not work; this overrides for all targets.
To that end, I tried something like the following:
# @fn set_target_dynamic_crt
# @brief Sets the given target to use the dynamic version of the CRT (/MD or
# /MDd)
# @param ... A list of targets to which this setting should be applied.
function( set_target_dynamic_crt )
if ( MSVC )
message (WARNING ${CMAKE_BUILD_TYPE})
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set_target_properties ( ${ARGN} PROPERTIES COMPILE_FLAGS "/MDd" )
else()
set_target_properties ( ${ARGN} PROPERTIES COMPILE_FLAGS "/MD" )
endif()
endif()
endfunction()
However, this always chooses the release version (/MD
) and when I query for the build type (the message
call above) I get the empty string. (I suspect this is because I'm using the Visual Studio generator; I've seen more than one reference that says CMAKE_BUILD_TYPE
is for makefiles only...)
How can I set compile options like this per target?