太多的评论但不是完整的答案:
我生成的项目中的一个典型序列是使用 CMake 中的CMakePackageConfigHelper和GNUInstallDirs模块。
无需生成foo-config-version.cmake
模板。只需使用write_basic_package_version_file
此模块中的命令即可完成工作。
export
如果您想使用构建目录中的配置包而不需要安装它们,该命令会派上用场。
参见示例:
# Support find_package(Foo NO_MODULE).
set(FOO_DOC_DIR ${CMAKE_INSTALL_DOCDIR})
set(FOO_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
set(FOO_LIB_DIR ${CMAKE_INSTALL_LIBDIR})
set(FOODIR ${CMAKE_INSTALL_PREFIX})
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
configure_package_config_file(foo-config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/foo-config.cmake"
INSTALL_DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
PATH_VARS FOODIR FOO_INCLUDE_DIR FOO_LIB_DIR FOO_DOC_DIR
)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/foo-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
# To make the component usable not only from the install directory but also from the build directory
export(
TARGETS Foo
FILE foo-export.cmake
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/foo-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/foo-config-version.cmake"
DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
COMPONENT development
)
install(EXPORT Foo
DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
NAMESPACE ${PROJECT_NAME}::
FILE foo-export.cmake
COMPONENT development
)