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

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)

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.

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