因此,我想创建一个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}")
我的问题是:
find_package
应该以哪种方式安装静态或共享的c库以查找(又称需要什么目的地)。FOO_LIBRARIES
之类的变量,以便find_package
函数在调用find_package
的项目中公开它们?答案 0 :(得分:0)
我想出了办法。基本上,您必须具有.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})