cmake别名的用处 [英] cmake usefulness of aliases
问题描述
我不太了解别名表达式的应用。
我知道我可以写这样的东西
I dont quite get the application of alias expressions. I understand that I can write something like this
cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
project(myLibs)
add_library(${PROJECT_NAME} src/test.cpp)
add_library(myLibs::myLibs ALIAS ${PROJECT_NAME})
...
,然后使用
target_link_libraries(${TARGET_NAME}
myLibs::myLibs
在另一个文件中将库链接到某些文件
in another file to link the library to some executable, etc.
但是我为什么要这样做呢?我还是跳过别名定义,直接使用内置库的目标名称
but why would I do that? I might as well skip the alias definition and just use the targetname of the built library directly
target_link_libraries(${TARGET_NAME}
myLibs
- 有人可以向我解释为什么存在别名
- 为什么使用::语法吗?别名不是完全任意的吗?
推荐答案
使用您的 add_library()
例如,CMa目标的名称例如直接链接到目标的输出文件名。
Taking your add_library()
example, the CMake target's name would e.g. directly be linked to the target's output file names.
因此 ALIAS
目标主要用于为目标提供更多拼写或结构化名称,例如添加一个命名空间。
So ALIAS
targets are mainly used to give the target a more spelling or structured name by e.g. adding a "namespace".
cmake-developer
文档提供有关命名空间的以下建议:
The cmake-developer
documentation gives the following advice on namespaces:
在提供导入目标时,应将它们命名为名称空间(因此
Foo ::
前缀); CMake将认识到传递给target_link_libraries()
且其名称中包含::
的值应被导入为目标(而不只是库名),并且如果该目标不存在,则会生成适当的诊断消息(请参阅策略CMP0028
)。
When providing imported targets, these should be namespaced (hence the
Foo::
prefix); CMake will recognize that values passed totarget_link_libraries()
that contain::
in their name are supposed to be imported targets (rather than just library names), and will produce appropriate diagnostic messages if that target does not exist (see policyCMP0028
).
这篇关于cmake别名的用处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!