您可以在正在安装的主配置文件中创建一个别名,而不是“安装”一个别名来导出给用户。
记住:主配置文件是你(作为项目的开发者)编写的,安装后find_package会找到该文件。对不同的命令使用EXPORT 选项,您只需要求 CMake 生成 additional 文件,这些文件将包含在主文件中。
fooConfig.cmake:
# Include the file generated by CMake. This would define IMPORTED target 'foo'.
include("${CMAKE_CURRENT_LIST_DIR}/fooTargets.cmake")
# Additional declarations for a user
# E.g. create an alias
add_library(foo_altname ALIAS foo)
CMakeLists.txt:
# ...
install(
TARGETS foo
EXPORT fooTargets)
# Assume all configuration files to be installed into lib/cmake/
install(
EXPORT fooTargets
DESTINATION "lib/cmake"
# Install a hand-written configuration file
install(
FILES fooConfig.cmake
DESTINATION "lib/cmake"
注意,如果别名只是一个前缀目标名称,那么您可以使用install(EXPORT) 命令的NAMESPACE 选项:
install(
EXPORT fooTargets
DESTINATION "lib/cmake"
NAMESPACE alt::
这将提供 IMPORTED 目标 all::foo 而不是普通的 foo。
有关创建配置文件的更多详细信息,请参阅documentation。