问题描述
我有一个CMakeLists,我想使用动态版本的C运行时和一些其他目标使用静态版本构建一些目标。
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.
因为这需要根据目标设置,设置 CMAKE_CXX_FLAGS_< Config>
的默认方法不起作用;
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()
但是,这总是选择版本( / MD
),当我查询生成类型(
message
调用上面)我得到空字符串。 (我怀疑这是因为我使用Visual Studio生成器;我看到多个参考,说 CMAKE_BUILD_TYPE
仅用于makefiles ...)
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?
推荐答案
2.8.12我添加了一个target_compile_options命令来满足这个需求:
In CMake 2.8.12 I added a target_compile_options command to address this need:
target_compile_options(tgt PRIVATE "/MD$<$<CONFIG:Debug>:d>")
请参阅
更多关于CMAKE_BUILD_TYPE以及生成器表达式更好的几个原因(例如IMPORTED目标配置映射)。
for more relating to CMAKE_BUILD_TYPE and several reasons why the generator expression is better (eg IMPORTED target config mapping).
这篇关于如何在使用CMake的特定构建配置中为特定目标设置特定的编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!