【问题标题】:Adding library dependencies to interface libraries in cmake在 cmake 中向接口库添加库依赖项
【发布时间】:2021-12-25 00:31:42
【问题描述】:

我有以下 cmake 文件

cmake_minimum_required(VERSION 3.16)

find_package(fmt)

add_library(mylib INTERFACE )
add_dependencies(mylib  fmt::fmt-header-only)
target_compile_features(mylib INTERFACE cxx_std_20)
target_include_directories(mylib INTERFACE .)

add_executable(test_exe test_exe.cpp)
target_link_libraries(test_exe PUBLIC mylib)

但是fmt 没有链接到test_exe,除非我明确地将它添加到依赖项中。我定义mylib的依赖是不是错了?

错误如下,如果我将fmt::header-only添加到test_exe的链接库,它就会消失

fatal error: 'fmt/format.h' file not found
#include <fmt/format.h>
         ^~~~~~~~~~~~~~

【问题讨论】:

  • 如果您想在与mylib 链接时传递与fmt::fmt-header-only 的链接,则使用target_link_libraries 而不是add_dependenciestarget_link_libraries(mylib INTERFACE fmt::fmt-header-only)
  • 谢谢!那效果很好。你能把它作为一个答案让我接受吗?

标签: c++ cmake


【解决方案1】:
add_dependencies(mylib  fmt::fmt-header-only)

只需确保目标 fmt::fmt-header-only 在构建 mylib 之前是最新的。无论mylib 的目标类型如何,它都不会链接fmt::fmt-header-only。通过target_link_libraries完成链接

target_link_libraries(mylib INTERFACE fmt::fmt-header-only)

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2012-12-21
    • 2019-06-24
    • 1970-01-01
    • 2018-05-27
    • 2014-09-15
    相关资源
    最近更新 更多