使用 cmake 安装动态库

Posted

技术标签:

【中文标题】使用 cmake 安装动态库【英文标题】:Installing dynamic library using cmake 【发布时间】:2020-11-02 19:03:36 【问题描述】:

我有自己编写的 C++/Qt 库。我知道对于它的分发,cmake 必须安装 .dll(或 Linux 的 .so)文件和头文件。于是我写了以下CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(my_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 COMPONENTS Core Network Sql REQUIRED)

set(HEADERS some_class.h)
set(SOURCES some_class.cpp)

add_library($PROJECT_NAME SHARED $HEADERS $SOURCES)

target_link_libraries($PROJECT_NAME Qt5::Core Qt5::Network Qt5::Sql)

install(TARGETS $PROJECT_NAME ARCHIVE DESTINATION "lib"
                                RUNTIME DESTINATION "bin"
                                COMPONENT library)
install(FILES $HEADERS DESTINATION "include")

我得到了想要在目录C:\Program Files (x86)\my_project中的文件:

-- lib/libmy_project.dll.a
-- bin/libmy_project.dll
-- include/some_class.h

但是,一旦我尝试使用测试程序链接我的库:

find_package(my_project REQUIRED)
# ....
target_link_libraries($PROJECT_NAME my_project) 

我有一个错误:

By not providing "Findmy_project.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "my_project", but CMake did not find one.

  Could not find a package configuration file provided by "my_project"
  with any of the following names:

    my_projectConfig.cmake
    my_project-config.cmake

  Add the installation prefix of "my_project" to CMAKE_PREFIX_PATH or
  set "my_project_DIR" to a directory containing one of the above
  files.  If "my_project" provides a separate development package or
  SDK, be sure it has been installed.

也就是说,他只是不知道去哪里找他。据我了解,CMake 有我的项目的配置文件,我的库的 CMakeLists.txt 应该以某种方式安装它。但是,我发现有关此帐户的互联网信息非常陡峭,没有解释和示例(没有寻求?)。不过,如何以及在哪里为库编写这些配置文件?

【问题讨论】:

Install有一个“EXPORT”选项,可以为你生成这样的配置文件,见cmake.org/cmake/help/v3.13/command/… 【参考方案1】:

我没有得到最终的理解,但我想出了要写什么才能正确导出库。

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(some_project VERSION 1.0 LANGUAGES CXX)

# Parameters initialization
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# Search for required libraries
find_package(Qt5 COMPONENTS Core Network Sql REQUIRED)

# Project files
set(HEADERS some_class.h)
set(SOURCES some_class.cpp)

# Building the library
add_library($PROJECT_NAME SHARED $HEADERS $SOURCES)

# Linking the necessary libraries
target_link_libraries($PROJECT_NAME Qt5::Core Qt5::Network Qt5::Sql)

# Setting which header files should be supplied with the library
set_target_properties($PROJECT_NAME PROPERTIES PUBLIC_HEADER "$HEADERS")

# Installing the library
install(TARGETS $PROJECT_NAME
        EXPORT "$PROJECT_NAME-config"
        RUNTIME DESTINATION "$CMAKE_INSTALL_BINDIR"
        LIBRARY DESTINATION "$CMAKE_INSTALL_LIBDIR/$PROJECT_NAME"
        ARCHIVE DESTINATION "$CMAKE_INSTALL_LIBDIR/$PROJECT_NAME"
        PUBLIC_HEADER DESTINATION "$CMAKE_INSTALL_INCLUDEDIR/$PROJECT_NAME"
        PRIVATE_HEADER DESTINATION "$CMAKE_INSTALL_INCLUDEDIR/$PROJECT_NAME"
        INCLUDES DESTINATION "$CMAKE_INSTALL_INCLUDEDIR/$PROJECT_NAME")

install(EXPORT "$PROJECT_NAME-config" DESTINATION "$CMAKE_INSTALL_LIBDIR/cmake/$PROJECT_NAME")

export(TARGETS $PROJECT_NAME FILE "$PROJECT_NAME-exports.cmake")

【讨论】:

以上是关于使用 cmake 安装动态库的主要内容,如果未能解决你的问题,请参考以下文章

CMake 学习四:CMake 构建静态库和动态库

CMake--静态库与动态库构建

如何使用cmake生成基于静态库的动态链接库

使用 cmake 如何静态链接一些库和动态链接其他库?

使用cmake/make打包Android 动态库

CMake:使静态库有效,但动态无效