Home > Enterprise >  Adding library dependencies to interface libraries in cmake
Adding library dependencies to interface libraries in cmake

Time:11-13

I have the following cmake file

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. Am I defining the dependencies of mylib wrong?

The error is the following and it goes away if I add fmt::header-only to the link libraries of test_exe

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

CodePudding user response:

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. It doesn't link fmt::fmt-header-only regardless of the target type of mylib. Linking is done via target_link_libraries

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