【问题标题】:Build dependency in CMake在 CMake 中构建依赖项
【发布时间】:2014-02-07 14:34:26
【问题描述】:

我想在我的应用程序中构建和使用静态链接的 Botan,这意味着首先

python configure.py --disable shared

紧随其后

make

然后我想为 libbotan.a 创建一个依赖目标作为“libbotan”,我可以在其余的 CMake 文件中使用它。由于 Botan 不使用 CMake,我不确定如何在我的项目中包含依赖项。

我目前的尝试是这样的:

在 deps/Botan.1.11.7/CmakeLists.txt

add_custom_command(OUTPUT botan-configure COMMAND ./configure.py --disable-shared)
add_custom_command(OUTPUT botan-build COMMAND make DEPENDS botan-configure)
add_custom_target(botan DEPENDS botan-configure botan-build)

但是当我像这样在 core/CMakeLists.txt 中添加 botan 作为依赖项时

add_executable(console console.cpp)
target_link_libraries(console messages core botan ${Boost_LIBRARIES})

我明白了

CMake Error at simpleclient/CMakeLists.txt:5 (target_link_libraries):
 Target "botan" of type UTILITY may not be linked into another target.  One
 may link only to STATIC or SHARED libraries, or to executables with the
 ENABLE_EXPORTS property set.

我尝试像这样使用 ExternalProject_Add

ExternalProject_Add(botan
    GIT_REPOSITORY https://github.com/randombit/botan.git
    CONFIGURE_COMMAND python configure.py --disable-shared
    BUILD_COMMAND make
    INSTALL_COMMAND make install
)

但这给了我同样的错误。

【问题讨论】:

    标签: cmake


    【解决方案1】:

    看看ExternalProject 模块:

    “ExternalProject_Add”函数创建一个自定义目标来驱动 下载、更新/修补、配置、构建、安装和测试 外部项目

    请注意,这只会创建一个实用程序目标。也就是说,您可以运行目标来构建库,并且可以将项目目标的依赖项添加到实用程序目标。您可以直接链接到目标。

    您仍然需要手动获取库的路径并包含外部项目的目录。由于有问题的项目本身似乎不使用 CMake,这意味着您自己编写对 find_libraryfind_path 等的调用,并使用这些调用的结果来正确集成依赖项。

    由于外部项目是作为正常 CMake 运行的一部分构建的,因此应该很容易通过在提供给 ExternalProject_Add 的安装路径中搜索来获得正确的值。

    【讨论】:

    • @dutt 我的错,扩大了答案。
    • "通过在提供给 ExternalProject_Add 的安装路径中搜索,应该很容易获得正确的值。"如果这很容易,为什么不解释这一步呢? :) 这对我来说并不明显......
    • @HannesLandeholm 我没有解释它,因为它高度依赖于您要添加的项目。我的意思是你必须在安装后手动检查安装文件夹的内容。基本上,看看它在安装目录中复制它的库和头文件的位置。由于安装树通常非常精简,这应该很简单,但是您的里程可能会因外部库而异。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2021-08-17
    • 2012-07-26
    相关资源
    最近更新 更多