# Importing dependencies for users of my library

**URL:** https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780
**Category:** Code
**Created:** [January 5, 2022, 3:58pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780 "2022-01-05T15:58:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [January 5, 2022, 3:58pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/1 "2022-01-05T15:58:03Z")

</div>

Hi!

I am trying to make my library user friendly, such that a user can import it as a target and link against it in a simple way. My library has a public 3rd party dependency that I can’t seem to configure quite right.

MyLibraryConfig.cmake:

```auto
include(CMakeFindDependencyMacro)

find_dependency(MyDependency)
include(${CMAKE_CURRENT_LIST_DIR}/MyLibrary.cmake)

```

The `find_dependency` macro invokes FindMyDependency.cmake, which looks like this:

```auto
find_library(MyDependency
...
)
...
add_library(MyDependency SHARED IMPORTED)
set_target_properties(MyDependency PROPERTIES 
  IMPORTED_LOCATION ${my_imported_location}
set_target_properties(MyDependency PROPERTIES
  IMPORTED_GLOBAL TRUE
)

```

MyLibrary.cmake includes, amongst other things, the line:

```auto
# Create imported target MyNamespace::MyLibrary
add_library(MyNamespace::MyLibrary SHARED IMPORTED)

set_target_properties(MyNamespace::MyLibrary PROPERTIES
  INTERFACE_INCLUDE_DIRECTORES ...
  INTERFACE_LINK_LIBRARIES "...;libMyDependency.so;..."

```

In my test project, I do the following:

```auto
find_package(MyLibrary REQUIRED)

target_link_libraries(TestProjectLibrary
  PUBLIC
    MyNamespace::MyLibrary
)

```

CMake doesn’t protest, but during linking I get:

```auto
/usr/bin/ld: cannot find -lMyDependency

```

Any ideas? Am I exporting/installing MyLibrary and its dependencies incorrectly, or is there a problem with how it’s imported?

---

<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: [January 5, 2022, 4:17pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/2 "2022-01-05T16:17:04Z")

</div>

> [@Dylan\_Reedy](#):
>
> `INTERFACE_LINK_LIBRARIES "...;libMyDependency.so;..."`

This should be the dependency target name, not its path directly. How are you using `target_link_libraries` for it in your project’s code?

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [January 5, 2022, 4:22pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/3 "2022-01-05T16:22:07Z")

</div>

The CMake code for my dependency is old and variable-oriented, so my project builds the dependency with `ExternalProject_Add` and links against it using variables:

```auto
target_link_libraries(MyLibrary
    PUBLIC
        $<BUILD_INTERFACE:${MYDEPENDENCY_LIBRARY}
        ....
        $<INSTALL_INTERFACE:${MYDEPENDENCY_LIBRARY_NAME}

```

where `MYDEPENDENCY_LIBRARY_NAME` is just the library with the path stripped out (thus the `INTERFACE_LINK_LIBRARIES` format you quoted).

A lot of that was guesswork, so I’m not surprised to find out that its potentially wrong… what should I do differently?

---

<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: [January 5, 2022, 4:24pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/4 "2022-01-05T16:24:15Z")

</div>

You should use the target in the main build as well. Using the path will just embed the build-time path into your package. Note that `ExternalProject_add` doesn’t mix well with `add_library` in the same project. Generally, you would want each project to build “on its own” and have a “superbuild” that does `ExternalProject_add` to coordinate building each component in order and passing the relevant information between them.

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [January 5, 2022, 4:31pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/5 "2022-01-05T16:31:53Z")

</div>

I have heard that those don’t generally mix well, but I’m stuck with `ExternalProject_Add` for my dependency due to the aforementioned old-fashioned CMake in their project. I’m not entirely sure “superbuild” is the right approach either, since my _customer_ does not want to or need to build MyDependency - they just need to link against it transitively when linking against MyLibrary.

Anyway - if I can represent my dependency as a target in my main project and link against that target, what’s next? Just add the dependency to my export set?

---

<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: [January 5, 2022, 8:13pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/6 "2022-01-05T20:13:25Z")

</div>

If you want to use variables everywhere, that’s fine; there should be no need to find the dependency as the full path should be part of your interface directly. However, it will not be relocatable. I would recommend:

- adding a `IMPORTED` target for the `ExternalProject_add` artifacts as you need; and
- making a `FindDependency.cmake` module for the dependency that makes a similarly named target (basically just do what that `IMPORTED` target does, but probably with `find_*` commands.

That _should_ get you to something which resembles Doing It Right™, but may need tweaking to patch over minor details.

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [January 5, 2022, 10:18pm UTC](https://discourse.cmake.org/t/importing-dependencies-for-users-of-my-library/4780/7 "2022-01-05T22:18:44Z")

</div>

Thanks, appreciate all the feedback on this. I’ll see what I can do!
