# Use cases for IMPORTED\_LINK\_DEPENDENT\_LIBRARIES

**URL:** <https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662>\
**Category:** Code\
**Created:** [May 12, 2022, 2:15pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662 "2022-05-12T14:15:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![friendlyanon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/friendlyanon/32/970_2.png) [@friendlyanon](https://discourse.cmake.org/u/friendlyanon)\
**Post date:** [May 12, 2022, 2:15pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/1 "2022-05-12T14:15:08Z")

</div>

I stumbled into this property while looking at generated export sets and I’m curious what it is used for exactly.

@ben.boeckel helpfully pointed me at commit [2cff26fa52cf9043f00d1efaaf31ab93e2db22e8](https://gitlab.kitware.com/cmake/cmake/-/commit/2cff26fa52cf9043f00d1efaaf31ab93e2db22e8) added by @brad.king in the C++ Slack, and he suggested that I ask about this here. ([Slack](https://cpplang.slack.com/archives/C5Y2GDECX/p1652362956704199))

My understanding of transitive dependency propagation prior to this was that I only needed to `find_dependency` private dependencies in the install interface when the project was built with `BUILD_SHARED_LIBS=0`. From the linked commit and the discussion on Slack it looks like I have to always `find_dependency` everything to be truly cross-platform in my install interfaces.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [May 12, 2022, 2:33pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/2 "2022-05-12T14:33:14Z")

</div>

This helps CMake generate `-rpath-link` flags to tell the linker where to find runtime dependencies of a shared library.

---

<div class="post-metadata">

**Author:** ![friendlyanon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/friendlyanon/32/970_2.png) [@friendlyanon](https://discourse.cmake.org/u/friendlyanon)\
**Post date:** [May 12, 2022, 3:02pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/3 "2022-05-12T15:02:11Z")

</div>

I see. So this isn’t just a Darwin thing. Would I be correct to assume then that adding `find_dependency` in the install interface should be unconditional for dependencies that were found using `find_package` during build and linked to `PRIVATE`ly? If we consider a 100% cross-platform scenario that is and just let CMake do the thing it’s good at.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [May 12, 2022, 3:15pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/4 "2022-05-12T15:15:22Z")

</div>

Yes, though in many cases the `-rpath-link` flag (or equivalent on another platform, if any) is not actually needed because the dependency’s libraries are installed in directories that the linker searches anyway. Many projects don’t bother. The code [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.23.1/Source/cmGeneratorTarget.cxx#L7407-7414) just ignores entries of `IMPORTED_LINK_DEPENDENT_LIBRARIES` that cannot be found.

---

<div class="post-metadata">

**Author:** ![friendlyanon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/friendlyanon/32/970_2.png) [@friendlyanon](https://discourse.cmake.org/u/friendlyanon)\
**Post date:** [May 12, 2022, 4:03pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/5 "2022-05-12T16:03:40Z")

</div>

Right.

For completeness’ sake, here is an SO answer describing the importance of `-rpath-link`: [linker - What's the difference between `-rpath-link` and `-L`? - Stack Overflow](https://stackoverflow.com/a/49181161)

It’s also more correct to unconditionally `find_dependency` private dependencies whose targets you depend on.

---

<div class="post-metadata">

**Author:** ![functor0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/functor0/32/2418_2.png) [@functor0](https://discourse.cmake.org/u/functor0)\
**Post date:** [May 12, 2022, 5:15pm UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/6 "2022-05-12T17:15:22Z")

</div>

I recently hit upon a related problem in that I had (manually) created my imported library targets A and B and needed `target_link_libraries(A PRIVATE B)` to work because my executable was failing to link without a computed `-rpath-link`. But of course, it doesn’t work currently because A is an imported target. It would have helped if it just populated `IMPORTED_LINK_DEPENDENT_LIBRARIES` instead of failing. Instead, I had to write my own wrapper to do this manually instead.

---

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [May 20, 2022, 8:55am UTC](https://discourse.cmake.org/t/use-cases-for-imported-link-dependent-libraries/5662/7 "2022-05-20T08:55:12Z")

</div>

It would be nice if there was a warning when imported targets have dependencies in `IMPORTED_LINK_DEPENDENT_LIBRARIES` but the targets do not exist.  
But that’s tracked in [https://gitlab.kitware.com/cmake/cmake/-/issues/22678](https://gitlab.kitware.com/cmake/cmake/-/issues/22678)
