How to handle header-only-libraries exported through INTERFACE_LINK_LIBRARIES?

Suppose I have some library foo that is exported, and depends on some header-only library target Bar::header-only privately:

add_library(foo)
target_link_libraries(foo
   PRIVATE
      Bar::header-only
)

Now, when exporting foo, its INTERFACE_LINK_LIBRARIES property will list Bar::header-only as a transitive link dependency, which I somehow would like to avoid; it is a header-only library no longer required for linking against foo once exported. How should this case be handled?

I looked at https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE.html#interface-link-libraries-direct-exclude but Iā€™m not sure whether it is the correct approach to take.

target_link_libraries(foo PRIVATE $<BUILD_INTERFACE:bar::header-only>)

1 Like