# How to handle transitive dependencies of a header-only library target?

**URL:** https://discourse.cmake.org/t/how-to-handle-transitive-dependencies-of-a-header-only-library-target/13517
**Category:** Usage
**Created:** [January 30, 2025, 1:38pm UTC](https://discourse.cmake.org/t/how-to-handle-transitive-dependencies-of-a-header-only-library-target/13517 "2025-01-30T13:38:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tonh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/3bc359/32.png) [@tonh](https://discourse.cmake.org/u/tonh)
#### Post date: [January 30, 2025, 1:38pm UTC](https://discourse.cmake.org/t/how-to-handle-transitive-dependencies-of-a-header-only-library-target/13517/1 "2025-01-30T13:38:10Z")

</div>

I have some header-only library dependency `Foo::foo` which I do not want to end up in the exported targets of my library. To prevent that from happening, the library is linked as follows:

```auto
target_link_libraries(my_library 
  PRIVATE
    $<BUILD_INTERFACE:Foo::foo>
)

```

This works, however `Foo::foo` itself depends on some libraries that I do want to be exported, since symbols of those libraries end up in the resulting library. Right now, I ensure those libraries are exported as follows:

```auto
get_property(_fooInterfaceLinkLibraries TARGET Foo::foo PROPERTY INTERFACE_LINK_LIBRARIES)
target_link_libraries(my_library
  PRIVATE
    $<BUILD_INTERFACE:Foo::foo>
    $<INSTALL_INTERFACE:${_fooInterfaceLinkLibraries}>
)

```

Is this the ‘canonical’ approach, or is there a simpler approach? Note; using `$<TARGET_PROPERTY:...>` in the `target_link_libraries` call does not work since the generator expression itself ends up in the exported targets CMake file, still requiring the `Foo::foo` target to be known by any consumer.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 30, 2025, 2:10pm UTC](https://discourse.cmake.org/t/how-to-handle-transitive-dependencies-of-a-header-only-library-target/13517/2 "2025-01-30T14:10:28Z")

</div>

If I understand correctly. Your application A is dependent on a header only library H, which depends on another lib L.  
A needs H only for be built but it exposes symbols from L.  
If that’s correct, A should link H privately AND L publicly.  
A is not supposed to have any knowledge of what H depends on. If it exposes a dependency, even if it is already linked by another one, you should link it.  
Theoretically, it could lead to multiple linking of L but, AFAIK, the link system (through help of cmake or not, I don’t know) will handle that smoothly.

The way you’re going seems to link against all dependencies of H, besides it will break if at some time you’re not dependent anymore to H but still to L.
