# Passing a CMake target to a library expecting a pre-installed library

**URL:** <https://discourse.cmake.org/t/passing-a-cmake-target-to-a-library-expecting-a-pre-installed-library/3059>\
**Category:** Code\
**Created:** [March 31, 2021, 10:20pm UTC](https://discourse.cmake.org/t/passing-a-cmake-target-to-a-library-expecting-a-pre-installed-library/3059 "2021-03-31T22:20:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![delthas](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/b9bd4f/32.png) [@delthas](https://discourse.cmake.org/u/delthas)\
**Post date:** [March 31, 2021, 10:20pm UTC](https://discourse.cmake.org/t/passing-a-cmake-target-to-a-library-expecting-a-pre-installed-library/3059/1 "2021-03-31T22:20:36Z")

</div>

Hi,

I’m building a project that builds and depends on two libraries, libA and libB. I’m adding both libraries very simply with `add_subdirectory(libA/)` and `add_subdirectory(libB/)`. I don’t own the code for libA and libB, they are external submodules.

libA and libB export CMake targets that I can refer to and use with `target_link_libraries(program INTERFACE libA libB)`

**My issue is that libB depends on libA.** This makes sense from the POV of libB, because libA is quite common, so on most systems, it will already be installed. But in my case, libA is not already installed on the system. I’m trying to build both libA, libB, and my project with a single CMake build, with nothing preinstalled.

**libB tries to find libA with find\_package.** libB has a FindLibA.cmake which calls find\_library and find\_path.

**What is the canonical, CMake-y way to make libB depend on my libA CMake target, instead of trying to look for libA on the disk?**

Currently the best I’ve found to work around this is to set in advance some `libA_LIBRARIES` and other variables to pre-fill the cache of the FindLibA.cmake script so that it doesn’t try to do anything, and return the name of the libA target instead. But it’s not ideal.

I feel like I’m missing something here, since I’m guessing this is quite a regular use case.

_(FWIW, libA is a TLS library, mbedTLS, and libB is libcurl)_

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [April 1, 2021, 5:11am UTC](https://discourse.cmake.org/t/passing-a-cmake-target-to-a-library-expecting-a-pre-installed-library/3059/2 "2021-04-01T05:11:23Z")

</div>

Since curl does not test for targets, it really needs a preinstalled library. In such cases I typically resort to superbuild using `ExternalProject`. One can easily ensure the right build order and install projects to a known location.

---

<div class="post-metadata">

**Author:** ![delthas](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/b9bd4f/32.png) [@delthas](https://discourse.cmake.org/u/delthas)\
**Post date:** [April 6, 2021, 9:13am UTC](https://discourse.cmake.org/t/passing-a-cmake-target-to-a-library-expecting-a-pre-installed-library/3059/3 "2021-04-06T09:13:54Z")

</div>

Ah, good point. Do you have any resource/guide on using ExternalProject for this kind of use case with only local folders?
