CMake的configure_package_config_file()是如何工作的?

kqhtkvqz  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(337)

此问题在此处已有答案

What's the use of configure_package_config_file option INSTALL_DESTINATION(1个答案)
3天前关闭。
我一直在关注CMake tutorial,当它讨论如何导出配置文件时,我对输出有点困惑。

# generate the config file that is includes the exports
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
  INSTALL_DESTINATION "lib/cmake/example"
  NO_SET_AND_CHECK_MACRO
  NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

我希望看到MathFunctionsConfig.cmake安装在像<some-directory>/lib/cmake/example/这样的地方。但是,我查看了安装目录和/usr/local/,也就是${CMAKE_INSTALL_PREFIX},我没有找到包含MathFunctionsConfig.cmake的示例文件夹。我找到MathFunctionsConfig.cmake的地方是在<installation-directory>/lib/cmake/MathFunctions中,由以下命令指定

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake
  DESTINATION lib/cmake/MathFunctions
)

那么configure_package_config_file做了什么,它只是生成MathFunctionsConfig.cmake而不是安装它吗?如果是这样,那么INSTALL_DESTINATION扮演的角色是什么?
如果有人能帮助我更好地理解这一点,我将非常感激。提前感谢!

fcg9iug3

fcg9iug31#

使用您正在使用的签名,您将得到略多于configure_file的签名。
INSTALL_DESTINATION选项用于确定如何根据配置脚本的位置找到安装目录的根目录。在大多数情况下,您将使用此选项与包含通过PATH_VARS相对于安装根目录的路径的变量名称一起使用。这将导致configure_package_config_file,这将导致cmake提供带有PACKAGE_前缀的列出的变量,以供在配置文件期间使用。

**注意:**这意味着您实际上应该传递configures脚本的安装目标,即在您的情况下为lib/cmake/MathFunctions

示例

CMakeLists.文本文件

set(MYLIB_INCLUDE_DIR include)

configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
    "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
    INSTALL_DESTINATION "lib/cmake/MathFunctions" # location relative to the install root the configured file will end up in
    PATH_VARS MYLIB_INCLUDE_DIR # allow us to use @PACKAGE_MYLIB_INCLUDE_DIR@ to refer to <install_prefix>/include in Config.cmake.in
    NO_SET_AND_CHECK_MACRO
    NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

Config.cmake.in

@PACKAGE_INIT@

set(MYLIB_INCLUDE_DIR "@PACKAGE_MYLIB_INCLUDE_DIR@") # absolute path available here

结果MathFunctionsConfig.cmake

####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was Config.cmake.in                            ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

####################################################################################

set(MYLIB_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include") # absolute path available here

相关问题