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



我有以下情况:项目 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 的 CMakeList 中.txt但它似乎不起作用,编译器在包含项目 B 的标头时也给了我错误,说它们不存在。

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

编辑:

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

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

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

  • 来源/
    -- CMakeLists.txt
    • 清岳/
      ---- External_ZLib.cmake
      ---- External_libxml2.cmake
    • Dowloads/(不需要创建这个目录,CMake 会为你做)
  • build/( 异源构建,防止乱扔垃圾源码树)

文件:
CMakeList.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
)

相关内容

  • 没有找到相关文章

最新更新