我正在使用CMake作为库,目标名为foo
。但是 - 我也希望库的用户能够将其称为foo_altname
.我尝试这样做:
add_library(foo_alt ALIAS foo)
install(
TARGETS foo foo_altname
EXPORT strf)
# etc. etc.
。但这会触发错误!:
CMake Error at CMakeLists.txt:78 (install):
install TARGETS given target "foo_altname" which is an alias.
我应该怎么做?
您可以在正在安装的主配置文件中创建别名,而不是"安装"用于导出给用户的别名。
请记住:是您(作为项目的开发人员(编写主配置文件,安装后将由find_package
找到。使用不同命令EXPORT
选项,您只需要求 CMake 生成其他文件,这些文件将包含在主文件中。
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"
请注意,如果别名只是前缀目标的名称,则可以对命令使用 NAMESPACE 选项install(EXPORT)
:
install(
EXPORT fooTargets
DESTINATION "lib/cmake"
NAMESPACE alt::
这将提供导入的目标all::foo
而不是普通foo
。
有关创建配置文件的更多详细信息,请参阅文档。