cmake中接口库添加库依赖

Adding library dependencies to interface libraries in cmake

我有以下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 不会 link 反对 test_exe 除非我明确地将它添加到依赖项中。我定义 mylib 的依赖关系错了吗?

错误如下,如果我将 fmt::header-only 添加到 test_exe

的 link 库中,错误就会消失
fatal error: 'fmt/format.h' file not found
#include <fmt/format.h>
         ^~~~~~~~~~~~~~
add_dependencies(mylib  fmt::fmt-header-only)

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

完成的
target_link_libraries(mylib INTERFACE fmt::fmt-header-only)