# Difference between linking external built and installed library

**URL:** https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369
**Category:** Code
**Created:** [December 14, 2020, 6:06pm UTC](https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369 "2020-12-14T18:06:14Z")
**Posts on this page:** 4
**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: [December 14, 2020, 6:06pm UTC](https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369/1 "2020-12-14T18:06:14Z")

</div>

Hi,

In my project `A` I use `ExternalProject_Add` function to build some third party library `B` and then I’m going to use this library inside my project `A`.

I guess there two options are available:

1. build `B` lib
2. build and install `B` lib

and then somehow link built or installed library `B` inside `A` project.

For few days I’ve been trying to follow the first option but the command `find_package(HDF5 REQUIRED)` used to fail (the error tells me that `hdf5-config.cmake` searches for a `/bin` folder in unexisted folder).

Now I think I should rather build-install `HDF5` and that should solve my problem.

The questions: is there a difference how developpers usually link external libraries to the target: do they build only or build-install those external libs?  
Is there a difference how those libraries are linked to the target depending on each build and build-install option?

---

<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 15, 2020, 4:24pm UTC](https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369/2 "2020-12-15T16:24:06Z")

</div>

Using `ExternalProject` to build project A and your own `add_library`/`add_executable` calls using the outputs of A is not well-supported. I suggest making your own project another `ExternalProject_add` call in the top-level so that it can use `find_package` during its configure time (knowing that the other external project will make it available first).

Cc: @craig.scott

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 15, 2020, 9:21pm UTC](https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369/3 "2020-12-15T21:21:33Z")

</div>

Mixing `ExternalProject_Add()` with other parts of your main project that build things directly can be quite inconvenient. You will likely end up having to hard-code names/locations of things instead of being able to let CMake find them. Ben’s suggestion of moving your own project’s code into its own `ExternalProject_Add()` is the more typical way to structure things when some parts must be built that way (basically if some parts need to be built via ExternalProject, make everything be built via ExternalProject). This would be the more conventional “superbuild” structure.

---

<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 15, 2020, 11:01pm UTC](https://discourse.cmake.org/t/difference-between-linking-external-built-and-installed-library/2369/4 "2020-12-15T23:01:42Z")

</div>

Thank you very much

I will follow your suggestion
