# Adding the interface of a library to the private include path of another target

**URL:** https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896
**Category:** Usage
**Created:** [August 5, 2025, 2:16pm UTC](https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896 "2025-08-05T14:16:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)
#### Post date: [August 5, 2025, 2:16pm UTC](https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896/1 "2025-08-05T14:16:26Z")

</div>

Hi,

is there a built-in method to add the (complete) _interface_ of a library to the _private_ include directories of another target (i.e. the include path used when _compiling_ that target)?

Example:

- lib1: non-`INTERFACE_ONLY` library which has `header1.h` in its interface
- lib2: non-`INTERFACE_ONLY` library which has `header2.h` in its interface; `header2.h`contains `#include "header1.h"`
- executable: one of its `.c` files contains `#include "header2.h"`; no _link_ dependency on lib2 or lib1

Approach:

```auto
get_target_property(lib2_interface lib2 INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(executable PRIVATE ${lib2_interface})

```

- Would this work _reliably_? I mean:
  - regardless of the order in which the lib1, lib2, and executable targets are defined
  - transitively (pulling in also the interface of lib1 without explicitly having to specify that dependency in the executable target)

- Is there a better method to add lib2’s (and transitively also lib1’s) interface directories to the include directories used when compiling executable?

Kind regards  
Ingolf

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [August 5, 2025, 2:22pm UTC](https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896/2 "2025-08-05T14:22:58Z")

</div>

Maybe, you can use the following pattern:

```cmake
target_link_libraries(executable PRIVATE "$<COMPILE_ONLY:lib2>")

```

For more information, see [$\<COMPILE\_ONLY:…\>](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#compile-context) help.

---

<div class="post-metadata">

### Author: ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)
#### Post date: [August 5, 2025, 2:42pm UTC](https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896/3 "2025-08-05T14:42:18Z")

</div>

> [@marc.chevrier](#):
>
> `target_link_libraries(executable PRIVATE "$<COMPILE_ONLY:lib2>")`

Thanks, Marc, for this hint. I had not been aware of that generator expression.

Unfortunately, we are currently more or less bound to using CMake 3.21.x, so I’d still be interested in an assessment as in my first question.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 20, 2026, 2:31pm UTC](https://discourse.cmake.org/t/adding-the-interface-of-a-library-to-the-private-include-path-of-another-target/14896/4 "2026-02-20T14:31:44Z")

</div>

I think that is OK, but `$<COMPILE_ONLY>` is really the long-term solution.

Note that your proposed method misses any _future_ additions to `lib2`’s include interfaces.
