Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding library dependencies to interface libraries in cmake

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading