连接Yaml-cpp和Armadillo共享库的CMake项目

时间:2017-11-24 11:46:39

标签: c++ cmake armadillo yaml-cpp

我在Linux上工作(第42.3章)。我想写一个CMakeList.txt文件,它创建我的共享库,包含我的代码以及yaml-cpp和armadillo库,以便在另一个代码中使用它。所以:

  • 首先从他们各自的软件包和CMakeList.txt文件中单独编译和安装Yaml-cpp和Armadillo,或者从我的CMake项目中一起构建和编译所有内容是不是更好?

目前我是按照第二种方式进行的,但是如果我做nm或ldd,我最终得到的是我的图书馆,它不能很好地连接yaml和armadillo。

我的项目结构如下:

在"上层"我有:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(myproject CXX)

set(CMAKE_INSTALL_PREFIX "../install" CACHE STRING
"Install path prefix, prepended onto install directories." FORCE)
mark_as_advanced(CMAKE_INSTALL_PREFIX)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I all my include files ")

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/all my src file directories)
add_subdirectory(src)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

find_package(Yaml REQUIRED)
add_library(Yaml INTERFACE IMPORTED)
set_property(TARGET Yaml PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${YAML_INCLUDE_DIR})
include_directories(${YAML_INCLUDE_DIRS})

find_package(BlasLapack)

find_package(Armadillo REQUIRED)
add_library(Armadillo INTERFACE IMPORTED)
set_property(TARGET Armadillo PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${ARMADILLO_INCLUDE_DIR})
include_directories(${ARMADILLO_INCLUDE_DIRS})

在"中等水平"我有:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

function (add_sources target)
get_property(prop_defined GLOBAL PROPERTY ${target}_SRCS DEFINED)
if(NOT prop_defined)
  define_property(GLOBAL PROPERTY ${target}_SRCS
  BRIEF_DOCS "Sources for the ${target} target"
  FULL_DOCS "List of source files for the ${target} target")
endif()

set(SRCS)
foreach(src IN LISTS ARGN)
 if(NOT IS_ABSOLUTE "${src}")
  get_filename_component(src "${src}" ABSOLUTE)
 endif()
 list(APPEND SRCS "${src}")
endforeach()

set_property(GLOBAL APPEND PROPERTY "${target}_SRCS" "${SRCS}")
endfunction()

add_subdirectory(all my src directories)

get_property(myproject_SRCS GLOBAL PROPERTY myproject_SRCS)
add_library(myproject SHARED ${myproject_SRCS})
install(TARGETS myproject DESTINATION lib)

最后在内心水平"我有:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

add_sources(myproject
            mysource.cpp)

install(FILES mysource.hpp DESTINATION include/myproject)

那么,缺少了什么?如何在我的网站中加入Yaml-cpp和Armadillo共享库?谢谢。

2 个答案:

答案 0 :(得分:0)

通常,CMake查找find_package(Yaml REQUIRED)之类的命令会设置YAML_LIBRARIES之类的变量。有关详细信息,请参阅源代码FindYAML.cmake

要与YAML共享库链接,请使用以下变量:

target_link_libraries(myproject ${YAML_LIBRARIES})

答案 1 :(得分:0)

最好使用柯南www.conan.io,C ++开发人员应该开始使用包管理器。然后你可以在你的conanfile.txt中定义

[requires]
armadillo/7.950.1@*****/stable
yaml-cpp/0.5.3@moonforged/stable

[generators]
cmake

犰狳包目前不在柯南存储库中,我有它自己的版本而尚未发布