gpt4 book ai didi

c++ - CMake 嵌套库

转载 作者:行者123 更新时间:2023-11-27 22:34:00 31 4
gpt4 key购买 nike

我有一个 C++ 项目,其中我的 executable 依赖于我的 core library 而那个库依赖于另一个外部库,libtorrent 如果它有任何相关性。

问题是我不知道如何设置我的 CMakeLists.txt,我已经搜索了几天,但无济于事。

主要思想是核心库可执行文件位于单独的git存储库中,因此该库不是可执行文件的子git模块。

我最好能够使用 ExternalProject_Add 将 core library 添加到 executable 但是当我这样做时,executable 提示说它对核心库再次使用的 libtorrent 一无所知。将 libtorrent 的 header 添加到可执行文件中是不够的,我还需要将 executable 链接到 libtorrent。但是为什么我要编译 core library,因为我只需要再次将 core library 的所有依赖项添加到 executable 中。

如果有人能指出正确的方向,告诉我如何设置一个使用具有其他依赖项的核心库的项目和一个使用核心库的可执行文件。

最佳答案

通常,这用目标和它们之间的链接来表示。

您的设置可能如下所示:

#  core cmake file
find_package(LibtorrentRasterbar REQUIRED)

add_library(core_lib file1.cpp file2.cpp file3.cpp ...)
target_link_libraries(core_lib PUBLIC LibtorrentRasterbar::torrent-rasterbar) # see below
# app cmake file

find_package(core_lib) # find it elsewhere

add_executable(app file1.cpp file2.cpp file3.cpp ...)
target_link_libraries(exec PRIVATE core_lib::core_lib)

如果 core 在它的头文件中需要新的库,那么你应该将它们添加到 core_lib 的依赖项中。任何公共(public)需求都会传递给用户,例如 app 目标。

与外部库或构建树的依赖关系使用find_library 表示。它可以是安装在您的根目录中的库,安装在您的用户目录中,安装在您的项目的子目录中,或者只是库的构建树。在您的情况下,找到构建树可能就是您想要的。

然后因为你的 core_lib 库位于另一个项目中,我建议去看看如何从构建树或安装中导出目标,所以 find_package(core_lib)会工作。


不幸的是,Libtorrent 似乎没有正确支持 CMake,因此找不到包 Libtorrent 和目标 Libtorrent::torrent-rasterbar 不会被定义。

有一些方法可以通过尝试他们的 FindLibtorrentRasterbar.cmake 来解决。

查看their find module ,很明显它不是用现代 cmake 制作的。如果文件支持链接到它们的目标,则必须在末尾添加这些行:

if(LibtorrentRasterbar_FOUND)
set(LibtorrentRasterbar_LIBRARY_DEPS "${LibtorrentRasterbar_LIBRARIES}")
list(REMOVE_ITEM LibtorrentRasterbar_LIBRARY_DEPS ${LibtorrentRasterbar_LIBRARY})

if(LibtorrentRasterbar_USE_STATIC_LIBS)
add_library(LibtorrentRasterbar::torrent-rasterbar STATIC IMPORTED GLOBAL)
else()
add_library(LibtorrentRasterbar::torrent-rasterbar SHARED IMPORTED GLOBAL)
endif()

set_target_properties(LibtorrentRasterbar::torrent-rasterbar PROPERTIES
IMPORTED_LOCATION ${LibtorrentRasterbar_LIBRARY}
INTERFACE_LINK_LIBRARIES ${LibtorrentRasterbar_LIBRARY_DEPS}
INTERFACE_COMPILE_DEFINITIONS ${LibtorrentRasterbar_DEFINITIONS}
INTERFACE_INCLUDE_DIRECTORIES ${LibtorrentRasterbar_INCLUDE_DIRS}
)
endif()

我没有提到的一个细节是 find_package 不会尝试查找您的依赖包。为此,创建一个如下所示的自定义配置文件:

# cmake/core_lib-config.cmake.in
include(CMakeFindDependencyMacro)

# this line is just like a find_package
# but made for transitivity
find_dependency(LibtorrentRasterbar)
include("${CMAKE_CURRENT_LIST_DIR}/core_lib-targets.cmake")

然后将导出更改为输出到 core_lib-targets.cmake 而不是通常的配置:

# this is the new config file with the add_dependency
configure_file(
cmake/core_lib-config.cmake.in
core_lib-config.cmake
@ONLY
)

# the new config file will include this target file
install(EXPORT core_lib_targets
NAMESPACE core_lib::
FILE core_lib-targets.cmake
DESTINATION lib/cmake/core_lib)

# export the current build tree
export(
EXPORT core_lib_targets
NAMESPACE core_lib::
FILE "${CMAKE_CURRENT_BINARY_DIR}/core_lib-targets.cmake"
)

关于c++ - CMake 嵌套库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186187/

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