# Can't export top level lib with dependency on subdir lib

**URL:** <https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084>\
**Category:** Usage\
**Created:** [April 23, 2020, 8:17pm UTC](https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084 "2020-04-23T20:17:17Z")\
**Posts on this page:** 3\
**Page:** 2

<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:** [May 6, 2020, 1:07am UTC](https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084/21 "2020-05-06T01:07:53Z")

</div>

> [@yisseamake](#):
>
> Since my library dependent on IMPORTED target, the consumers of my library has to use find\_package to find (=IMPORT) in their CMake files

They should just need to do `find_package(yourprogram)`. `yourprogram-config.cmake` does `find_package()` for your dependencies to bring in their imported targets. I usually add them to `cmake` in the source tree and beside `yourpackage-config.cmake` in the build/install tree. Just scope it so your Find modules don’t interfere with the caller’s.

```cmake
set(CMAKE_MODULE_PATH_save "${CMAKE_MODULE_PATH}")
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_LIST_DIR}")

# Any find_packages you need.

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH_save}")
unset(CMAKE_MODULE_PATH_save)

```

> [@yisseamake](#):
>
> What if external packages does not provide FooConfig.cmake but provides foo.pc file? Should not then pkg\_search\_module /pkg\_check\_module be used instead of find\_package /writing FooFind.cmake? What are pro and contra points?

I’d write a `FindFoo.cmake` that does the `pkg_search_module` inside of it.

---

<div class="post-metadata">

**Author:** ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)\
**Post date:** [May 6, 2020, 1:25pm UTC](https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084/22 "2020-05-06T13:25:06Z")

</div>

The case for my own library was clear( generated yourprogram-config.cmake) , but I was questioning case when why library depends on external target for which I had to create either a) FindSublib.cmake or b) process Sublib-supplied pkg config.pc file. So as you suggested b) should used inside a).

With this I observed for a 3rd party sublib, .pc file contains lib and include paths that different ( match path the build was done) than the actual path on my system and when I modified path inside .pc file , pkg\_search\_module still sets pkg\_LIBRARY\_DIR and \_INCLUDE to wrong path value. I see some Makefile in the siblib tree has this path , but is not pkg\_search\_module supposed to take it only from .pc file?

---

<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:** [May 7, 2020, 3:25pm UTC](https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084/23 "2020-05-07T15:25:34Z")

</div>

Do you have actual changes to show as an example? I’m not getting a clear picture of what you’re encountering from your description.

[Previous page](https://discourse.cmake.org/t/cant-export-top-level-lib-with-dependency-on-subdir-lib/1084.md?page=1)
