# Expressing runtime dependencies in CMake

**URL:** https://discourse.cmake.org/t/expressing-runtime-dependencies-in-cmake/2235
**Category:** Code
**Created:** [November 25, 2020, 7:21am UTC](https://discourse.cmake.org/t/expressing-runtime-dependencies-in-cmake/2235 "2020-11-25T07:21:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Joachim\_Priesner](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9fc348/32.png) [@Joachim\_Priesner](https://discourse.cmake.org/u/Joachim_Priesner)
#### Post date: [November 25, 2020, 7:21am UTC](https://discourse.cmake.org/t/expressing-runtime-dependencies-in-cmake/2235/1 "2020-11-25T07:21:27Z")

</div>

Hi all,

I am working on a project making excessive use of run-time library loading, i.e. dlopen()/LoadLibrary().

This means that if target B uses library A, it needs:

- at compile time: the include directories of A
- at link time: nothing
- at load time: nothing
- at run time: the shared library A to be present

Currently I do the following:  
add\_library(A.SharedLib SHARED …)

add\_library(A INTERFACE)  
target\_include\_directories(A INTERFACE path/to/A/includes)  
add\_dependencies(A A.SharedLib) # make sure that if a target depending on A is built, then A.SharedLib is also built so that it is available at run time

target\_link\_libraries(B PRIVATE A)

This works, but it has the drawback of non-optimal parallelization: A and B could be built in parallel, but thanks to add\_dependencies(), A must be completely built and linked before B starts to build.

So: is there a way to tell CMake that target B needs target A only at runtime, or in other words:

- if B is built, A must be brought up to date as well
- but B does not need A at compile or link time

We use Visual Studio, Makefile and Ninja as generators. So adding another “dummy” target that depends on both B and A is probably out because developers would still build and run the executable target A from their VS environment and not the dummy target.

---

<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: [November 25, 2020, 5:33pm UTC](https://discourse.cmake.org/t/expressing-runtime-dependencies-in-cmake/2235/2 "2020-11-25T17:33:01Z")

</div>

> [@Joachim\_Priesner](#):
>
> We use Visual Studio, Makefile and Ninja as generators. So adding another “dummy” target that depends on both B and A is probably out because developers would still build and run the executable target A from their VS environment and not the dummy target.

I think this is the best you can do right now. CMake doesn’t know _why_ that dependency exists, so it assumes that it is needed before anything happens in B (generated headers and the like).

I have written somewhere about runtime usage requirements, but I haven’t done any work towards it (or found the post…probably on a CMake issue).
