I have a header-only library that depends on some other library.
I would like to define my library in a way that targets linking to it also link to its dependencies.
This is the code I have looks something like this:
# Library
add_library(my_lib INTERFACE)
target_link_libraries(my_lib INTERFACE other_lib)
target_include_directories(my_lib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
# Executable
add_executable(my_executable main.cpp)
target_link_libraries(my_executable PRIVATE my_lib)
With the code above, building my_executable
gives linker error related to other_lib
.
Linking other_lib
explicitly works fine:
target_link_libraries(my_executable PRIVATE my_lib other_lib)
But I’d like to avoid that and have CMake sort that out automatically without my_executable
having to explicitly reference other_lib
. How would I achieve this?