在CMake中构建依赖关系

dut*_*utt 8 cmake

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

python configure.py --disable shared
Run Code Online (Sandbox Code Playgroud)

其次是

make
Run Code Online (Sandbox Code Playgroud)

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

我目前的尝试看起来像这样:

in 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)
Run Code Online (Sandbox Code Playgroud)

但是当我将botan添加为core/CMakeLists.txt中的依赖项时,就像这样

add_executable(console console.cpp)
target_link_libraries(console messages core botan ${Boost_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)

我明白了

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.
Run Code Online (Sandbox Code Playgroud)

我试过像这样使用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
)
Run Code Online (Sandbox Code Playgroud)

但这给了我同样的错误.

Com*_*sMS 11

看一下ExternalProject模块:

'ExternalProject_Add'功能创建一个自定义目标来驱动外部项目的下载,更新/补丁,配置,构建,安装和测试步骤

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

您仍然需要获取库的路径并手动包含外部项目的目录.由于有关项目似乎并没有单独使用CMake的,这意味着编写自己的呼吁find_library,find_path等等,并使用结果从这些调用适当整合的依赖.

由于外部项目是作为正常CMake运行的一部分构建的,因此通过搜索给定的安装路径来获取正确的值应该非常容易ExternalProject_Add.

  • “通过搜索给定的ExternalProject_Add的安装路径应该很容易获得正确的值。” 如果这么简单,为什么不简单解释一下这一步呢?:) 这对我来说并不明显...... (2认同)