gpt4 book ai didi

cmake get find_package 查找包特定变量

转载 作者:行者123 更新时间:2023-11-30 16:18:58 27 4
gpt4 key购买 nike

因此,我想创建一个 C 库,该库可以通过 cmake 中的 find_package 函数轻松地在其他项目中使用。具体来说,我想要一个名为 foo 的项目,可以像这样访问:

find_package (foo-1.0 REQUIRED)

message ("!-- foo found version \"${FOO_VERSION}\"")
message ("!-- foo include path \"${FOO_INCLUDE_DIRS}\"")
message ("!-- foo libraries \"${FOO_LIBRARIES}\"")

然后,这些变量可以与其他 cmake 项目中的目标一起使用,例如:

add_executable (example example.c)
target_include_directories (example PRIVATE "${FOO_INCLUDE_DIRS}")
target_link_libraries (example PRIVATE "${FOO_LIBRARIES}")

我的问题是:

  1. 应该以什么方式安装静态或共享 C 库才能被 find_package 找到(又名需要什么目的地)。
  2. 如何公开 FOO_LIBRARIES 等变量,以便 find_package 函数在调用 find_package 的项目中公开它们?

最佳答案

我想出了如何做到这一点。基本上,您必须有 .cmake.in 文件作为最终包的配置文件和配置版本文件的模板。 find_package 将在以下位置查找 cmake 配置和版本文件:

${CMAKE_INSTALL_PREFIX}/include/${LIBKOF_NAMED_VERSION}
${CMAKE_INSTALL_PREFIX}/lib/${LIBKOF_NAMED_VERSION}

其中LIBKOF_NAMED_VERSION是包文件夹,例如foo-1.2

libkof 是我的包的名称,您可以在下面看到这是如何完成的:

# This cmake is responsible for installing cmake config and other version
# files.

# This sets the package specific versioning
set(LIBKOF_MAJOR_VERSION 1)
set(LIBKOF_MINOR_VERSION 0)
set(LIBKOF_PATCH_VERSION 0)

# This allows easy creation of the directories within /usr/local or another install
# prefix
set(LIBKOF_NAMED_VERSION libkof-${LIBKOF_MAJOR_VERSION}.${LIBKOF_MINOR_VERSION})

# These statements will create the directories needed for installs
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${LIBKOF_NAMED_VERSION})
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIBKOF_NAMED_VERSION})

# This tracks the name of the in files used to generate the package cmake files
set(LIBKOF_CONFIG_FILE libkof-config.cmake.in)
set(LIBKOF_CONFIG_VERSION_FILE libkof-config-version.cmake.in)

# The configure_file statements will exchange the variables for the values in this cmake file.
configure_file(${LIBKOF_CONFIG_FILE} libkof-config.cmake @ONLY)
configure_file(${LIBKOF_CONFIG_VERSION_FILE} libkof-config-version.cmake @ONLY)

# Installs to the output locations so they can be found with find_package()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libkof-config-version.cmake DESTINATION include/${LIBKOF_NAMED_VERSION})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libkof-config.cmake DESTINATION lib/${LIBKOF_NAMED_VERSION})

关于cmake get find_package 查找包特定变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55752143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com