- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
运行 pip install cmake 时出现上述错误在 alpine linux (WSL) 上。 完整错误: /home/user# pip install cmake Collecting c
我的项目依赖 mariadb-connector-c我正在尝试使用 cmake 自动化下载、构建和链接过程。 我目前将项目下载到一个目录中,然后尝试执行生成忍者文件并运行它们,但我根本无法运行 cma
我正在尝试像这样在 cmake 中执行 execute_process execute_process(COMMAND ${CMAKE_COMMAND} -P myScript.cmake 这仅在文件
我想知道如果我没有给它任何变量告诉它在哪里找到它,cmake 如何找到我的 llvm cmake 配置。 我是 LLVM 新手。我正在构建一个 Hello World LLVM pass。我在 Ubu
我正在开发一个使用 CMake 的项目。顶CMakeLists.txt文件包含以下行: cmake_minimum_required(VERSION 3.7.2) # Kittens will die
我正在使用 Meego 1.2 工具链使用 cmake 运行构建(长话短说)。这个特定的工具链需要使用 --sysroot 选项来查找它需要的任何库。 set (CMAKE_CXX_FLAGS "-f
我们有一些源代码处理工具,可以从多个“片段”生成原始汇编文件等内容。 当使用 make 中的这些工具时,我们可以通过让源处理工具发出“依赖文件”来确保最新的构建,就像 gcc 一样-MD 标志。 例如
如何在 MSYS2 中安装和配置“cmake”?我尝试安装以下 MSYS 包 我在运行任何 cmake 命令时都收到以下错误 '''CMakeLists.txt:5 (cmake_minimum_re
有没有办法在 CMake 中得到一个变量列表?具体来说,我想要做的是调用一个现有函数,该函数接受多个变量并检查它们的计算结果是否为 true。 在某些情况下,其中一些变量将是空列表(计算结果为 fal
我有两组测试(功能测试和单元测试),我希望能够指定通过 cmake 运行哪一组。 一组测试是我想通过“make test”运行的单元测试。 另一组测试是我想通过“制作功能测试”来运行的功能测试。 目前
我的目标是查看有关调用 g++ 的详细信息由 cmake 直接调用从命令行。我不关心 make 的输出就这个问题而言。 根据official FAQ和 the accepted answer on a
我有一个生成库的CMake项目。我已经编写了一个CMake脚本FindXXX.cmake来帮助我的图书馆的用户。这样,他们可以使用find_package(XXX)并获取所需的变量。到现在为止还挺好。
当我看到带有命名空间的 CMake 库时,它们总是在表单中Parent::Component . 如果我有一个足够大的库,那么该库的子部分可能包含组件。我想知道是否可以/适合做 ParentProje
CMake 变量和属性似乎完成了非常相似的事情,我一直无法理解它们之间的区别。 它们每个都有自己的文档部分,但两者都会影响构建系统,两者都是“预先存在的”,并且两者都可以基于其他 CMake 命令动态
我有一个包含多个子目录的 CMake 项目,例如: dir1 subdir11 subdir12 dir2 subdir21 subdir22 根 CMakeLists
如果您想将静态库链接到共享库或可执行文件,同时保持所有符号可见(例如,您可以稍后打开它以找到它们),在 Linux/BSD 上执行此操作的一种非可移植方式是使用标志-Wl,--whole-archiv
CMake 自动创建一个 FooTarget.cmake文件例如添加 install(EXPORT FooTargets FILE FooTargets.cmake NAMESPACE Foo
在CMake中,ELSE和ENDIF控制流函数将表达式作为参数。根据文档,这些是optional。那么这些目的是什么?仅仅是为了维护目的而使原始IF表达式更清晰,还是提供某些功能? 最佳答案 正如您所
我是 cmake 的新手,我发现它非常令人沮丧。我试图在构建运行时评估的文件路径中使用通配符,而不是在生成构建时。 我创建了一个构建,它使用 SWIG 为某些 C++ 代码生成 Java 包装器。我可
我可能在谷歌上搜索错误,但我无法找到 .cmake 文件的用途。 我刚刚偶然发现了一个我必须使用的项目的 CMake 工具,我很难理解它是如何工作的。我明白在包含 CMakeLists.txt 的目录
我是一名优秀的程序员,十分优秀!