带有相对路径的CMake安装目标

时间:2019-06-12 03:36:50

标签: c++ cmake

我试图弄清楚如何编写一个库,该库可以通过在另一个项目中调用find_package()来使用。我正在尝试关注Catch2存储库CMake file

这是我提出的最低版本:( git repo可用here

cmake_minimum_required(VERSION 3.14.4)

set(project_name "hello")
project(${project_name} LANGUAGES CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

add_library(${project_name} STATIC)

target_sources(${project_name} PRIVATE "src/hello.cpp")

target_include_directories(${project_name}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src/>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

install(
    DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/src/
    DESTINATION include
    FILES_MATCHING REGEX "src/.+\.h"
    PERMISSIONS
        OWNER_READ
        GROUP_READ
        WORLD_READ)

set(PROJECT_CMAKE_CONFIG_DESTINATION ${CMAKE_INSTALL_LIBDIR}/../)
configure_package_config_file(
    ${CMAKE_CURRENT_LIST_DIR}/cmake/${project_name}Config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/${project_name}Config.cmake
    INSTALL_DESTINATION
        ${PROJECT_CMAKE_CONFIG_DESTINATION})

install(
    TARGETS
        ${project_name}
    EXPORT
        ${project_name}Targets
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib)

install(
    EXPORT
        ${project_name}Targets
    NAMESPACE
        ${project_name}::
    DESTINATION
        ${PROJECT_CMAKE_CONFIG_DESTINATION})

install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/${project_name}Config.cmake"
    DESTINATION
        ${PROJECT_CMAKE_CONFIG_DESTINATION})

然后我安装时正在使用:

- cmake -DCMAKE_INSTALL_PREFIX:PATH="C:\tmp\install"
- cmake --build . --install target

,所有内容似乎都被复制到正确的路径。但是,当我尝试使用另一个消耗我的测试库的项目中的find_package()时,出现以下错误:

CMake Error at C:/tmp/install/helloTargets.cmake:76 (message):
  The imported target "hello::hello" references the file

     "C://lib/hello.lib"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "C:/tmp/install/helloTargets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  C:/tmp/install/helloConfig.cmake:33 (include)
  CMakeLists.txt:15 (find_package)

似乎缺少cmake安装目录的路径。如何修改安装功能,以便find_package()可以使用生成的配置文件?

1 个答案:

答案 0 :(得分:1)

在您的CMakeLists.txt中,现阶段有些奇怪的事情:

  1. target_include_directories(${project_name}
        PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src/> 
            # there is no header in this folder so this has no effect
            # maybe create a include/ folder to make it more "legit"
            $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
    
  2. 您应该在 target_sources 中的.cpp中指定头文件。这有2种效果:

    • 执行 install(TARGETS hello)时,可以添加 PUBLIC_HEADER 关键字,将属于目标的每个标头安装到$ {CMAKE_INSTALL_INCLUDEDIR}。这样,您无需使用正则表达式进行安装(目录)
    • IDE将向他们显示
  3. 在helloConfig.cmake.in

if(NOT TARGET hello::hello)

我认为,当您使用configure_package_config_file()时,生成的导出文件具有一定的逻辑以防止多重包含。

最后,我认为您的主要问题是由于PROJECT_CMAKE_CONFIG_DESTINATION的位置,如果删除最后一个“ /”,我认为它应该可以解决问题。

编辑13.06: 由于PROJECT_CMAKE_CONFIG_DESTINATION设置为“ lib /../”,因此计算所有相对路径的基本路径是错误的 some doc about how CMake computes the relative path

您尝试使用“ $ {CMAKE_INSTALL_LIBDIR} /../”实现的实际上是“。”因此基本路径为$ {CMAKE_INSTALL_PREFIX}。 另一种选择是安装在$ {CMAKE_INSTALL_LIBDIR} / cmake这样的子文件夹中,或者也可以只在“ cmake”文件中安装