# Why do private linked targets leak out in install targets ?

**URL:** https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076
**Category:** Usage
**Created:** [November 27, 2024, 2:52pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076 "2024-11-27T14:52:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 27, 2024, 2:52pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/1 "2024-11-27T14:52:32Z")

</div>

I have added a make install target to my library  
, and this library links private against another one.  
There are no build or runtime dependencies to other one.

Why do I need the other one in the export targets? How can I avoid that?  
(the library linking against contains just a set of warnings and flags that are totally private and shall not leak out)

PS: The problem occourse only if the dependent library is fetched via fetch\_content

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [November 27, 2024, 7:04pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/2 "2024-11-27T19:04:31Z")

</div>

See [CMake Error: install(EXPORT "ProjectTargets" ...) includes target "xxx" which requires target "yyy" that is not in any export set. - #6 by ben.boeckel](https://discourse.cmake.org/t/cmake-error-install-export-projecttargets-includes-target-xxx-which-requires-target-yyy-that-is-not-in-any-export-set/7344/6)

---

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 27, 2024, 7:32pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/3 "2024-11-27T19:32:45Z")

</div>

Thank you, interesting.

`a4z::commonCompilerWarnings` is the lib that is fetched  
and it works, but wants the cmake files of commonCompilerWarnings in the install target.

changing like in the thread

```auto
target_link_libraries(sl3
    PRIVATE
# a4z::commonCompilerWarnings
    "$<BUILD_LOCAL_INTERFACE:a4z:commonCompilerWarnings>"
)

```

This change adds a `-la4z::commonCompilerWarnings` to the linker line, and that does of course not work (using the non generator expression does not do that)

Maybe I understand the solution wrong?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [November 27, 2024, 7:39pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/4 "2024-11-27T19:39:52Z")

</div>

I use `BUILD_INTERFACE` very often like this:

```cmake
target_link_libraries(${TARGET_LIBRARY} PUBLIC $<BUILD_INTERFACE:${TARGET_LIBRARY}_project_options>)

```

---

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 27, 2024, 9:17pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/5 "2024-11-27T21:17:09Z")

</div>

Does not work for me.

I leave it as is until I have the source online.  
Then it will be easier to talk about.

---

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 28, 2024, 1:08pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/6 "2024-11-28T13:08:57Z")

</div>

Ok, I can confirm this works with internal libraries that are defined in the project.

But it does not work with a library that I want to reuse like this one: [GitHub - a4z/commonCompilerWarnings: Always the same CMake warnings as a library](https://github.com/a4z/commonCompilerWarnings)

other project shall use this, and link against it, so there is one central place to manage warnings and other flags.

If I do a fetch content for this, or consume it via vcpkg, it lands in the export target, even if it is also linked PRIVAT, with or without $\<BUILD\_INTERFACE or $\<BUILD\_LOCAL\_INTERFACE …

That is kind of inconsistent and unexpected, and it makes my idea about how to share compiler and linked flags between projects a not soo good one, even if is _should_ work

Would it make sense to report that as a bug to CMake, or is that for some reasons I do not see expected behaviour?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [November 28, 2024, 1:22pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/7 "2024-11-28T13:22:32Z")

</div>

I guess the **OVERRIDE\_FIND\_PACKAGE** cause the problems. If it is installed, it is imported?

---

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 28, 2024, 2:47pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/8 "2024-11-28T14:47:22Z")

</div>

I do not think it is just the **OVERRIDE\_FIND\_PACKAGE**  
If commoncompilerWarnings comes via cmake, its exactly the same

say I have a library sl3, that consumes commonCompilerWarnings like this

```auto
target_link_libraries(sl3
    PRIVATE
    a4z::commonCompilerWarnings
)

```

that’s what can be found in `libsl3Targets.cmake`

```auto
set_target_properties(a4z::sl3 PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "\$<LINK_ONLY:a4z::commonCompilerWarnings>;/Applications/Xcode-16.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.1.sdk/usr/lib/libsqlite3.tbd"
)

```

that at least can work when the dependency is either available via cmake, or sl3 install calls the install of commonCompilerWarnings and commonCompilerWarnings cmake files are also there.

$\<BUILD\_INTERFACE or $\<BUILD\_LOCAL\_INTERFACE … can not be used at all if the library comes via fetch\_content of vcpkg (like it would be in the system), since this adds something to the linker line `-la4z::commonCompilerWarnings`

That’s all very surprising, unexpected, and not optimal.

PS: and I tried with OVERRIDE\_FIND\_PACKAGE, FetchContent\_MakeAvailable and other variations, did not change anything dramatic

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [November 28, 2024, 4:08pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/9 "2024-11-28T16:08:25Z")

</div>

I have tested it and it works file, too if it is installed:

> <https://github.com/ClausKlein/cmake-init-modules/blob/557a0da524d62cd43c0bf628029495eeb04859d7/CMakeLists.txt#L81>

---

<div class="post-metadata">

### Author: ![a4z](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@a4z](https://discourse.cmake.org/u/a4z)
#### Post date: [November 28, 2024, 6:37pm UTC](https://discourse.cmake.org/t/why-do-private-linked-targets-leak-out-in-install-targets/13076/10 "2024-11-28T18:37:47Z")

</div>

WOW !  
Now, when copying it like this, it works indeed.

Cool, thank you so much!
