在CMake中使用别名目标 [英] Using alias targets in CMake

查看:546
本文介绍了在CMake中使用别名目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义CMake目标时,可以创建别名目标,以便在后续命令中可以使用别名来引用已定义的目标。例如

When defining CMake targets one can create alias targets so that the alias name can be used to refer to the defined target in subsequent commands. For instance

add_library(foo_lib foo.cpp bar.cpp bat.cpp)
add_library(foo::lib ALIAS foo_lib)

据我了解,这样做的好处是名称 foo_lib 不会显示为make目标。但是,给定这样的别名,我想为它们设置各种属性,例如:

As far as I understood it, this has the advantage that the name foo_lib doens't appear as a make target. However, given such an alias name I'd like to set all kinds of properties to them such as:

set_target_properties(foo::lib PROPERTIES COMPILE_DEFINITIONS ...)
target_include_directories(foo::lib PUBLIC ... PRIVATE ...)

,但是不幸的是,这是不可能的,因为 CMake错误:set_target_properties不能用于ALIAS目标。我不知道为什么这不可能,因为我想一次定义lib的名称,并在每次要调整目标的属性时都引用给定的别名。关于如何正确使用ALIAS目标的任何提示?

but this is not possible unfortunately, since CMake Error: set_target_properties can not be used on an ALIAS target. I don't see why this shouldn't be possible as I'd like to define the name of my lib once and refer to the given alias whenever I want to adjust a property of the target. Any hints on how to use ALIAS targets "correctly"? What is the purpose of ALIAS targets other than they're not appearing as Make targets then?

推荐答案

ALIAS em>类似于同义词。 ALIAS 目标只是原始目标的另一个名称。因此,对 ALIAS 目标的要求是不可修改-您无法调整其属性,进行安装等。

ALIAS similar to "synonym". ALIAS target is just another name for original one. So the requirement for ALIAS target to be non-modifiable - you cannot adjust its properties, install it, etc.

创建别名的一种可能情况-具有目标,目标与原始目标有所不同,但有效地相同(例如,在给定的配置中):

One of possible scenario for creating alias - having target, which conceptionally differ from original one, but effectively the same (e.g., in given configuration):

if(FOO_USE_SHIPPED)
    add_library(FOO ...) # Library FOO shipped with our project
endif()

...

# We need FOO_test for testing
if(FOO_USE_SHIPPED)
    add_library(FOO_test ALIAS FOO) # Use our library
else()
    add_library(FOO_test IMPORTED)
    set_target_property(FOO_test ...) # Use external library
endif()

这篇关于在CMake中使用别名目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆