【问题标题】:How to add libraries installed with ExternalProject_Add to target includes如何将使用 ExternalProject_Add 安装的库添加到目标包括
【发布时间】:2018-08-11 15:40:38
【问题描述】:

我正在尝试通过ExternalProject_Add 将外部库包含到 cmake 项目中。为了试用这个功能,我创建了一个最小的工作示例,其中涉及使用ExternalProject_Add 将 pugixml 添加到项目中。但是,我在寻找从外部项目的本地安装(即 pugixml 的头文件)到项目的包含路径中添加库的头文件的方法时遇到问题。

最小工作示例的项目树组织如下:

.
├── build
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    └── main.cpp

在此项目树中,build 指的是构建目录和调用 cmake 生成构建的路径。

./CMakeLists.txt的内容如下:

cmake_minimum_required(VERSION 3.0)

include(ExternalProject)

ExternalProject_Add(pugixml
        GIT_REPOSITORY https://github.com/zeux/pugixml.git
        INSTALL_DIR ${PROJECT_BINARY_DIR}/extern_install/pugixml
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
)

add_subdirectory(src)

在上面的示例中,我将 pugixml 添加为外部项目,该项目将安装在项目的二进制目录中,其文件将由存储在 ./src 中的可执行文件使用。因此,./src/CMakeLists.txt 的内容为:

project(foo)

add_executable(foo main.cpp)
target_link_libraries(foo ${pugixml_LIBRARIES})
include_directories(${pugixml_INCLUDE_DIR}) # this part doesn't work

这正是我遇到问题的部分。我假设一旦添加并安装了外部项目,ExternalProject_Add 就会定义一些便利库来帮助引用库文件和包含目录。但是,这是行不通的。有谁知道使用ExternalProject_Add 包含外部库的正确方法是什么?

【问题讨论】:

标签: c++ cmake external-project


【解决方案1】:

不幸的是,这根本行不通。外部项目的构建是在构建时而不是在 CMake 配置/生成期间完成的。提示是The ExternalProject_Add function creates a custom target to drive download, update/patch, configure, build, install and test steps of an external project: (from CMake)

因此,您必须自己定义所有变量。另请参阅:CMake - linking to library downloaded from ExternalProject_add()(实际上我会将其标记为链接问题的副本,因为问题相同)

顺便说一句:我也不喜欢这样做的方式。这种方式不能简单地使用find_package等。

使用它的预期方法是“超级构建项目”:定义 1 个 CMakeLists,其中您只有 ExternalProject_Adds 用于所有依赖项和您的项目。然后它将与find_package 一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2017-05-21
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多