CMake链接到调试和发布中仅释放目标的配置 [英] CMake link to only release config of target in both debug and release
问题描述
还有另一种方法可以在将目标包含在用于发布和调试配置的 target_link_libraries
中时,仅链接目标的发布库.
Is there another a way to link only the release lib of a target when including the target with target_link_libraries
for both release and debug configs.
我知道 target_link_libraries
具有 optimize
和 debug
选项,并且可以这样做
I know target_link_libraries
has the options optimize
and debug
and that it can be done like this
target_link_libraries(current_target
optimized $<TARGET_PROPERTY:lib_target,IMPORTED_IMPLIB_RELEASE>
debug $<TARGET_PROPERTY:lib_target,IMPORTED_IMPLIB_RELEASE>
)
但是我通常将目标保留在列表中
However I generally keep the targets in a list
set(target_list
lib_target1
lib_target2
...
)
,然后在同一列表上执行其他操作,例如将目标的二进制目录包含在调试的搜索路径中.使用 optimized
和 debug
选项也不允许lib_target ...属性通过current_target传递.我可以解决这个问题,只是想知道是否还有其他方法?
and I perform other things on the same list, like getting the binary directory of the target to include in the search path for debugging. Using the optimized
and debug
options also do not allow the lib_target... properties to be passed along through the current_target. I can work around it just curious if there is another way?
推荐答案
如果与IMPORTED目标链接,则其依赖于配置的属性将指导入的配置".您可以随时在项目的配置和导入的配置之间调整映射:
If you link with IMPORTED target, then its configuration-dependent properties refer to "imported configuration". You may always adjust mapping between configurations of your project and imported ones:
-
全局配置映射通过以下设置将为每个导入目标的 Release 配置,用于 Release , Debug 或<您的项目的em> RelWithDebugInfo 配置:
The setting below will use Release configuration of every IMPORTED target for any of Release, Debug or RelWithDebugInfo configurations of your project:
set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release) set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Release) set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release)
-
每个目标配置映射由 MAP_IMPORTED_CONFIG_< CONFIG> 属性.
请注意,这些设置应在创建导入目标之前发布.也就是说,如果此类目标是通过 find_package()
调用创建的,则设置应位于这些调用之前.
Note, that these settings should be issued before creating of IMPORTED targets. That is, if such targets are created with find_package()
calls, the settings should precede these calls.
下面的设置与上面的全局设置相同,但仅适用于 lib_target1
IMPORTED目标:
Settings below do the same as global settings above but only for lib_target1
IMPORTED target:
set_target_properties(lib_target1 PROPERTIES
MAP_IMPORTED_CONFIG_RELEASE Release
MAP_IMPORTED_CONFIG_DEBUG Release
MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release)
只有在创建给定的导入目标后,才能应用这些设置.在 find_package()
调用之后.
These settings can be applied only after given IMPORTED target is created, e.g. after find_package()
call.
值得一提的是,您还可以指定 fallback 导入的配置:
It is worth to mention, that you may also specify fallback imported configurations:
set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Release Debug)
使用这种设置,如果您的项目是在 Debug 配置中构建的,并且某些IMPORTED目标没有 Release 配置,则其 Debug >将使用配置.(但是,如果该目标既没有Release配置也没有Debug配置,则CMake将发出错误).
With such setting, if your project is built in Debug configuration, and some IMPORTED target doesn't have Release configuration, then its Debug configuration will be used. (But if that target has neither Release nor Debug configuration, CMake will emit an error).
这篇关于CMake链接到调试和发布中仅释放目标的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!