如何在自定义目录中安装cmake包并按名称将其与target_link_libraries()链接?

时间:2017-10-16 14:18:47

标签: c++ cmake

我有一个自定义库,可以导出包含include/的{​​{1}},lib/lib/cmake/,其中包含以下内容:

MyProjectConfig.cmake

我在另一个项目中使用它:

set(MyProject_INCLUDE_DIRS "/home/.../cmake-build-debug/thirdparty/myproj/include")
set(MyProject_LIBRARY_DIRS "/home/.../cmake-build-debug/thirdparty/myproj/lib")
set(MyProject_LIBRARIES "MyProject")

message(STATUS "MyProject found. Headers: ${MyProject_INCLUDE_DIRS}")

我希望cmake会自动选取include(ExternalProject) ExternalProject_Add(MyProjectExternal PREFIX "${CMAKE_BINARY_DIR}/external/myproject" GIT_REPOSITORY "git@bitbucket.org:myproject.git" GIT_TAG "master" CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/thirdparty/myproject ) add_dependencies(${PROJECT_NAME} MyProjectExternal) # prevent error on first project use when dir doesn't exist if(EXISTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib) find_package(MyProject REQUIRED HINTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib/cmake) find_library(MyProject_LIB MyProject HINTS ${MyProject_LIBRARY_DIRS}) target_link_libraries(${PROJECT_NAME} PUBLIC ${MyProject_LIB}) target_include_directories(${PROJECT_NAME} PRIVATE ${MyProject_INCLUDE_DIRS}) endif() 中设置的变量,以便按名称查找库,这样可行:

MyProjectConfig.cmake

但它没有:

find_package(MyProject REQUIRED HINTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib/cmake)
target_link_libraries(${PROJECT_NAME} PUBLIC MyProject)

第二部分 as requested here The full code and the steps to reproduce the problem

图书馆代码(mylib branch

的CMakeLists.txt

[ 87%] Built target MyProjectExternal
[ 90%] Linking CXX executable RootProject
/usr/bin/ld: cannot find -lMyProject
collect2: error: ld returned 1 exit status

的cmake / install.cmake

cmake_minimum_required(VERSION 3.8)
project(MyLib VERSION 1.0.0 LANGUAGES CXX)

add_library(${PROJECT_NAME} SHARED
    src/mylib/hello.cpp
    )
target_include_directories(${PROJECT_NAME}
    PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
    )

set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
    )

include(cmake/install.cmake)

的cmake / Config.cmake.in

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets
    ARCHIVE  DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY  DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME}
    EXPORT_MACRO_NAME EXPORT
    NO_EXPORT_MACRO_NAME NO_EXPORT
    PREFIX_NAME MYLIB_
    EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include-exports/mylib/export.h)
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include-exports>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(DIRECTORY ${CMAKE_BINARY_DIR}/include-exports/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

include(CMakePackageConfigHelpers)
set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${PROJECT_VERSION})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION ${PROJECT_VERSION_MAJOR})
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${PROJECT_VERSION_MAJOR})
write_basic_package_version_file(
    "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}ConfigVersion.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)
export(EXPORT ${PROJECT_NAME}Targets
    FILE "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}.cmake"
)
SET(CONFIG_SOURCE_DIR ${CMAKE_SOURCE_DIR})
SET(CONFIG_DIR ${CMAKE_BINARY_DIR})
SET(${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_SOURCE_DIR}/include")
configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/Config.cmake.in
    "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}Config.cmake"
    INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
    PATH_VARS ${PROJECT_NAME}_INCLUDE_DIR)
install(EXPORT ${PROJECT_NAME}Targets
    FILE ${PROJECT_NAME}.cmake
    DESTINATION lib/cmake/${PROJECT_NAME}
)
install(
    FILES
        "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}Config.cmake"
        "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}ConfigVersion.cmake"
    DESTINATION lib/cmake/${PROJECT_NAME}
    COMPONENT Devel
)

包括/ MYLIB / hello.h

@PACKAGE_INIT@

set_and_check(@PROJECT_NAME@_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
set_and_check(@PROJECT_NAME@_LIBRARY_DIRS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@")
set(@PROJECT_NAME@_LIBRARIES "@PROJECT_NAME@")

check_required_components(@PROJECT_NAME@)

message(STATUS "@PROJECT_NAME@ found. Headers: ${@PROJECT_NAME@_INCLUDE_DIRS}")

的src / MYLIB / HELLO.CPP

#ifndef MYLIB_HELLO_H
#define MYLIB_HELLO_H

#include <mylib/export.h>

namespace mylib {
    extern MYLIB_EXPORT void hello();
}

#endif

申请代码(master branch

的CMakeLists.txt

#include <mylib/hello.h>
#include <iostream>

namespace mylib {
    void hello() {
        std::cout << "Hello, MyLib!" << std::endl;
    }
}

的main.cpp

cmake_minimum_required(VERSION 3.8)
project(MyLibConsumer VERSION 1.0.0 LANGUAGES CXX)

add_executable(${PROJECT_NAME}
    main.cpp)

set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
    )

include(ExternalProject)
set(LIB_INSTALL_DIR ${CMAKE_BINARY_DIR}/thirdparty/mylib)
ExternalProject_Add(MyLibExternal
    PREFIX "${CMAKE_BINARY_DIR}/external/mylib"
    GIT_REPOSITORY "https://github.com/arteniioleg/stackoverflow-question-46772541.git"
    GIT_TAG "mylib"
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
    )
add_dependencies(${PROJECT_NAME} MyLibExternal)
if(EXISTS ${LIB_INSTALL_DIR}/lib) # prevent error on first cmake load
    find_package(MyLib REQUIRED HINTS ${LIB_INSTALL_DIR}/lib/cmake)

    # fixme: make this work
    target_link_libraries(${PROJECT_NAME} PUBLIC MyLib)

    # to test: comment the above line and uncomment the below lines
    #find_library(MyLib_LIB MyLib HINTS ${MyLib_LIBRARY_DIRS})
    #target_link_libraries(${PROJECT_NAME} PRIVATE ${MyLib_LIB})
    #target_include_directories(${PROJECT_NAME} PRIVATE ${MyLib_INCLUDE_DIRS})
endif()

错误

#include <mylib/hello.h>

int main()
{
    mylib::hello();
    return 0;
}

使用main.cpp:1:25: fatal error: mylib/hello.h: No such file or directory #include <mylib/hello.h> ^ 创建的MyLib_*变量可以解决问题,但问题太详细了。

根据建议here

  

但是,推荐的方法是使用CMake功能创建配置文件。这样,将创建完全成熟的目标,并可用于链接。

怎么做?

我想分两步使用我的库,比如Qt:

  1. find_package()
  2. find_package(Qt5Widgets)

2 个答案:

答案 0 :(得分:0)

  

我预计cmake会自动选取MyProjectConfig.cmake中设置的变量,以便按名称查找库。

find_package命令中最“神奇”的是它搜索*Config.cmake脚本的方式。找到脚本后,只需在调用者的上下文中执行。

在您的情况下,CMake设置变量MyProject_INCLUDE_DIRSMyProject_LIBRARY_DIRSMyProject_LIBRARIES没有更多。它不会创建 MyProject 目标等。

如果您希望find_package()准备与MyProject库建立关联,则需要相应地编写MyProjectConfig.cmake脚本(例如,从中调用link_directories())。

但是,推荐的方法是使用CMake功能创建配置文件。这样,将创建完全成熟的目标,并可用于链接。有关详细信息,请参阅cmake-packages documentation

答案 1 :(得分:0)

只需在"biodata": [{ "Ruby": { "Expertise": "web development", "EXperience": "5 years" }}, {"Dylon": { "Expertise": "Java", "EXperience": "2 years" } }] 中加入MyProject.cmake(默认不包含)

MyProjectConfig.cmake

密钥文件的最终版本:

LIBRARY / cmake的/ Config.cmake.in

删除了多余的变量:include(${CMAKE_CURRENT_LIST_DIR}/MyProject.cmake) *_INCLUDE_DIRS*_LIBRARY_DIRS

*_LIBRARIES

EXECUTABLE /的CMakeLists.txt

在可执行文件之前移动了外部项目,如果没有构建外部项目,则移动@PACKAGE_INIT@ include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@.cmake) check_required_components(@PROJECT_NAME@) message(STATUS "@PROJECT_NAME@ found.")

return()