cmake文件应安装在哪里?

时间:2019-04-25 08:54:37

标签: cmake

cmake文件应安装在哪里?我目前有安装目标

cmake_minimum_required(VERSION 2.8.10)
project(projectname)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

add_library(projectnameINTERFACE)
target_include_directories(projectnameINTERFACE
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/projectname>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
)


configure_package_config_file(projectnameConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/projectnameConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS projectnameEXPORT projectname-targets)

install(EXPORT projectname-targets FILE projectnameTargets.cmake DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/projectnameConfig.cmake DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake)
install(DIRECTORY ./ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/projectname FILES_MATCHING PATTERN "*.h" PATTERN ".git" EXCLUDE)

路径${CMAKE_INSTALL_PREFIX}/share/projectname/cmake是否正确?

对于Windows构建,应与CMAKE_INSTALL_PREFIX=C:/libs/project一起安装。当使用CMAKE_INSTALL_PREFIX=/usr/usr/local在Linux上安装它时,我不确定cmake文件是否应该安装在/usr/share/cmake或类似文件夹中。

最后,它应该以合理的方式使用两种方法(/usrpath/to/libdir)。

1 个答案:

答案 0 :(得分:2)

根据官方的cmake文档,有几种可能性可以安装这些cmake文件。为了使用find_package()命令查找库,该位置很重要。取决于平台,可能的位置是:

<prefix>/                                                       (Windows)
<prefix>/(cmake|CMake)/                                         (Windows)
<prefix>/<name>*/                                               (Windows)
<prefix>/<name>*/(cmake|CMake)/                                 (Windows)
<prefix>/(lib/<arch>|lib*|share)/cmake/<name>*/                 (Unix)
<prefix>/(lib/<arch>|lib*|share)/<name>*/                       (Unix)
<prefix>/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/         (Unix)
<prefix>/<name>*/(lib/<arch>|lib*|share)/cmake/<name>*/         (Windows/Unix)
<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/               (Windows/Unix)
<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/ (Windows/Unix)
<prefix>/<name>.framework/Resources/                            (macOS)
<prefix>/<name>.framework/Resources/CMake/                      (macOS)
<prefix>/<name>.framework/Versions/*/Resources/                 (macOS)
<prefix>/<name>.framework/Versions/*/Resources/CMake/           (macOS)
<prefix>/<name>.app/Contents/Resources/                         (macOS)
<prefix>/<name>.app/Contents/Resources/CMake/                   (macOS)

前缀由CMAKE_INSTALL_PREFIX变量定义,在Windows中默认为C:/Program Files/${PROJECT_NAME},在Unix中默认为/usr/local

在我的Arch Linux系统上,大多数库都使用<prefix>/lib*/cmake/<name>*/(Unix风格)位置。对于Windows个人,我更希望使用<prefix>/cmake位置。

有关更多详细信息,我建议您阅读CMake版本的官方文档(有关v3.14,请参见下面参考中的链接)。

参考

  1. https://cmake.org/cmake/help/v3.14/variable/CMAKE_INSTALL_PREFIX.html
  2. https://cmake.org/cmake/help/v3.14/command/find_package.html?highlight=find_package#id5