简体   繁体   English

在 cmake 中为接口库添加库依赖项

[英]Adding library dependencies to interface libraries in cmake

I have the following cmake file我有以下 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)

But fmt is not linked against test_exe unless I explicitly add it to the dependencies.但是fmt不会与test_exe链接,除非我明确地将它添加到依赖项中。 Am I defining the dependencies of mylib wrong?我是否错误地定义了mylib的依赖项?

The error is the following and it goes away if I add fmt::header-only to the link libraries of test_exe错误如下,如果我将fmt::header-only添加到test_exe的链接库,它就会消失

fatal error: 'fmt/format.h' file not found
#include <fmt/format.h>
         ^~~~~~~~~~~~~~
add_dependencies(mylib  fmt::fmt-header-only)

simply makes sure that the target fmt::fmt-header-only is up to date before mylib is built.只需确保在构建mylib之前目标fmt::fmt-header-only是最新的。 It doesn't link fmt::fmt-header-only , INTERFACE library or not.它不链接fmt::fmt-header-onlyINTERFACE库与否。 Linking is done via target_link_libraries链接是通过target_link_libraries完成的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM