CMakeLists配置链接两个C ++项目

时间:2015-03-16 16:29:21

标签: c++ cmake include-path

我有以下情况:项目A依赖于项目B,但两者都是同时构建的。项目A必须包括项目B的包含,并且还需要链接其库。到目前为止,我已经尝试过这种方式:

 ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/other_project other_project)

然后:

INCLUDE_DIRECTORIES(includ ${CMAKE_SOURCE_DIR}/other_project/include})
LIST(APPEND LINK_LIBS other_project)
项目A的CMakeLists.txt中的

但它似乎不起作用,编译器在包含项目B的标题说它们不存在时也给我错误。

在A中添加依赖项的正确方法是什么? CMakeLists.txt应该怎么样?

编辑:

根据评论中的建议,这个问题在this中得到解决,但是我想看一个如何在CMakeList.txt文件中使用它的示例。

1 个答案:

答案 0 :(得分:1)

以下是一个简单的示例,它构建zlib然后构建libxml2,这取决于我们构建的zlib。有一点需要注意,我很快从我以前做过的事情中提取了这个例子。 libxml2示例使用make,因此实际上只会在拥有它的系统上构建,例如Linux,Mac ......

以下是此示例的建议目录结构...

  • 的src /
    - CMakeLists.txt
    • 的CMake /
      ---- External_ZLib.cmake
      ---- External_libxml2.cmake
    • Dowloads /(无需创建此目录,CMake将为您执行此操作)
  • build /(源代码构建以防止乱丢源树)

文件:
CMakeLists.txt

# Any version that supports ExternalProject should do
cmake_minimum_required( VERSION 3.1)

project(test_ext_proj)

set(test_ext_proj_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
set(CMAKE_MODULE_PATH ${test_ext_proj_CMAKE_DIR} ${CMAKE_MODULE_PATH})

set(test_ext_proj_BUILD_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install)

set(test_ext_proj_DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Downloads CACHE PATH "Directory to store downloaded tarballs.")

include(ExternalProject)
include(External_ZLib)
include(External_libxml2)

External_ZLib.cmake:

set(ZLib_version 1.2.8)
set(ZLib_url "http://zlib.net/zlib-${ZLib_version}.tar.gz")
set(ZLib_md5 "44d667c142d7cda120332623eab69f40")

ExternalProject_Add(ZLib
  URL ${ZLib_url}
  URL_MD5 ${ZLib_md5}
  PREFIX ${vision-tpl_BUILD_PREFIX}
  DOWNLOAD_DIR ${test_ext_proj_DOWNLOAD_DIR}
  INSTALL_DIR ${test_ext_proj_BUILD_INSTALL_PREFIX}
  CMAKE_GENERATOR ${gen}
  CMAKE_ARGS
    -DCMAKE_INSTALL_PREFIX:PATH=${test_ext_proj_BUILD_INSTALL_PREFIX}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
)

#This variable is required so other packages can find it.
set(ZLIB_ROOT ${test_ext_proj_BUILD_INSTALL_PREFIX} CACHE PATH "" FORCE)

External_libxml2.cmake:

set(libxml2_release "2.9")
set(libxml2_patch_version 0)
set(libxml2_url "ftp://xmlsoft.org/libxml2/libxml2-sources-${libxml2_release}.${libxml2_patch_version}.tar.gz")
set(libxml2_md5 "7da7af8f62e111497d5a2b61d01bd811")

#We need to state that we're dependent on ZLib so build order is correct
set(_XML2_DEPENDS ZLib)

#This build requires make, ensure we have it, or error out.
if(CMAKE_GENERATOR MATCHES ".*Makefiles")
  set(MAKE_EXECUTABLE "$(MAKE)")
else()
  find_program(MAKE_EXECUTABLE make)
  if(NOT MAKE_EXECUTABLE)
    message(FATAL_ERROR "Could not find 'make', required to build libxml2.")
  endif()
endif()

ExternalProject_Add(libxml2
  DEPENDS ${_XML2_DEPENDS}
  URL ${libxml2_url}
  URL_MD5 ${libxml2_md5}
  PREFIX  ${test_ext_proj_BUILD_PREFIX}
  DOWNLOAD_DIR ${test_ext_proj_DOWNLOAD_DIR}
  INSTALL_DIR  ${test_ext_proj_BUILD_INSTALL_PREFIX}
  BUILD_IN_SOURCE 1
  CONFIGURE_COMMAND ./configure
    --prefix=${test_ext_proj_BUILD_INSTALL_PREFIX}
    --with-zlib=${ZLIB_ROOT}
  BUILD_COMMAND ${MAKE_EXECUTABLE}
  INSTALL_COMMAND ${MAKE_EXECUTABLE} install
)