gpt4 book ai didi

c++ - 链接 Yaml-cpp 和 Armadillo 共享库的 CMake 项目

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:08 28 4
gpt4 key购买 nike

我在 Linux (leap 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 共享库合并到我的库中?谢谢。

最佳答案

通常,CMake 查找命令如 find_package(Yaml REQUIRED) 设置变量如 YAML_LIBRARIES。详情见源码FindYAML.cmake

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

target_link_libraries(myproject ${YAML_LIBRARIES})

关于c++ - 链接 Yaml-cpp 和 Armadillo 共享库的 CMake 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472658/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com