# Use external libraries that are already linked to a subproject

**URL:** <https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269>\
**Category:** Code\
**Created:** [November 30, 2020, 8:32pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269 "2020-11-30T20:32:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [November 30, 2020, 8:32pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269/1 "2020-11-30T20:32:08Z")

</div>

Hi,

Let’s suppose I have a project with two subfolders and **subPrj1** links **libhdf5\_D.lib** :  
 ![image](https://discourse.cmake.org/uploads/default/original/1X/9f28fa27baaf200d11c9dbc4d1788c858058889d.png)

Now I want to link **subPrj1** to **subPrj2** and use the same **libhdf5\_D.lib** in **subPrj2**.  
What is the “cleanest” way to do that? Do I have to manually link **subPrj1** and **libhdf5\_D.lib** to **subPrj2** or it is enough to simply link **subPrj1** to **subPrj2**?

---

<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:** [December 1, 2020, 1:15pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269/2 "2020-12-01T13:15:25Z")

</div>

`subPrj1` has the HDF5 information on its `PUBLIC` interface, so it will be given to consumers of `subPrj1` automatically. You should be able to do `target_link_libraries(subPrj2 PRIVATE subPrj1)` (feel free to use `PUBLIC` if needed here) and have HDF available in subPrj2.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 1, 2020, 1:35pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269/3 "2020-12-01T13:35:45Z")

</div>

And do I also have to include HDF5 headers to subPrj2 via `target_include_directories(subPrj2 PRIVATE ${H5_DIR}/include)` ? or it is enough `target_include_directories(subPrj2 PRIVATE subPrj1/include)`?

---

<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:** [December 1, 2020, 4:21pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269/4 "2020-12-01T16:21:06Z")

</div>

The usage requirements will come along with `subPrj1`, so HDF5’s `PUBLIC` include directory will come along. I recommend adding `$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>` to `subPrj1` so that `target_link_libraries` is completely sufficient.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 1, 2020, 5:00pm UTC](https://discourse.cmake.org/t/use-external-libraries-that-are-already-linked-to-a-subproject/2269/5 "2020-12-01T17:00:21Z")

</div>

Thank you a lot! that works
