在CMake中构建依赖关系

时间:2014-02-07 14:34:26

标签: cmake

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

python configure.py --disable shared

接着是

make

然后我想为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)

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

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
)

但这给了我同样的错误。

1 个答案:

答案 0 :(得分:10)

查看ExternalProject模块:

  

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

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

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

由于外部项目是作为普通CMake运行的一部分构建的,因此通过搜索ExternalProject_Add的安装路径可以很容易地获得正确的值。