# Register dependencies between \`all\` targets

**URL:** <https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278>\
**Category:** Code\
**Created:** [October 14, 2021, 8:45am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278 "2021-10-14T08:45:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)\
**Post date:** [October 14, 2021, 8:45am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/1 "2021-10-14T08:45:35Z")

</div>

Hi,

my project has the following hierarchy:

```auto
toplevel
 +-foo # added with add_subdirectory(... EXCLUDE_FROM_ALL)
 +-bar # added with add_subdirectory(...)
    +-quux # added with add_subdirectory(... EXCLUDE_FROM_ALL)

```

Apparently, there is an `all` target both in `toplevel/foo` and `toplevel/bar/quux`; due to the `EXCLUDE_FROM_ALL` these `all` targets are just not registered as dependencies in the `all` targets of the corresponding parent folders.

Is it possible to have the `toplevel/bar/quux/all` target depend on `toplevel/foo/all`? Alternatively, is there another way to build all in `foo` whenever all in `bar/quux` is built?

I tried the obvious `add_dependencies(toplevel/bar/quux/all toplevel/foo/all)`, but CMake won’t let me use `toplevel/bar/quux/all` in this context.

Kind regards

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 14, 2021, 10:03am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/2 "2021-10-14T10:03:36Z")

</div>

Use `CMake` targets rather than internal `CMake` artifacts:

```cmake
add_dependencies(quux foo)

```

---

<div class="post-metadata">

**Author:** ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)\
**Post date:** [October 14, 2021, 1:04pm UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/3 "2021-10-14T13:04:55Z")

</div>

@marc.chevrier where would I place this `add_dependencies(...)` instruction? Neither `bar/CMakeLists.txt` nor `bar/quux/CMakeLists.txt` seems to be the right place:

> CMake Error at bar/CMakeLists.txt:2 (add\_dependencies):  
> Cannot add target-level dependencies to non-existent target “quux”.

> CMake Error at bar/quux/CMakeLists.txt:2 (add\_dependencies):  
> Cannot add target-level dependencies to non-existent target “quux”.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 14, 2021, 2:14pm UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/4 "2021-10-14T14:14:34Z")

</div>

At a level where **both** targets are already defined.

---

<div class="post-metadata">

**Author:** ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)\
**Post date:** [October 15, 2021, 7:18am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/5 "2021-10-15T07:18:11Z")

</div>

@marc.chevrier, thanks for your recommendations. I’m sorry, but I still don’t get it right:

`toplevel/CMakeLists.txt` reads:

```auto
add_subdirectory(foo EXCLUDE_FROM_ALL)
add_subdirectory(bar)

```

`toplevel/foo/CMakeLists.txt`:

```auto
add_executable(hello main.c)
# possibly more targets to be defined here...

```

`toplevel/bar/CMakeLists.txt`:

```auto
add_subdirectory(quux EXCLUDE_FROM_ALL)
add_dependencies(quux foo) # both foo and quux folders have been visited by now

```

`toplevel/bar/quux/CMakeLists.txt`:

```auto
add_executable(world main.c)
# possibly more targets to be defined here...

```

CMake still reports

> CMake Error at bar/CMakeLists.txt:2 (add\_dependencies):  
> Cannot add target-level dependencies to non-existent target “quux”.

Moving the `add_dependencies()` instruction to the end of the toplevel `CMakeLists.txt` does not work either. Neither does changing `quux` to `bar/quux` in the moved `add_dependencies()`.

Kind regards

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 15, 2021, 9:53am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/6 "2021-10-15T09:53:28Z")

</div>

You cannot use directories as targets. So `foo` or `quux` cannot be specified as arguments to `add_dependencies()`.

In your case, you can specify, for example, `add_dependencies(world hello)`

---

<div class="post-metadata">

**Author:** ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)\
**Post date:** [October 15, 2021, 10:23am UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/7 "2021-10-15T10:23:12Z")

</div>

Thank you very much, @marc.chevrier.

This is what I was afraid of. In my real-life case, the situation is a little more complex: there is not just one `hello` and one `world` target, so with the suggested approach, I would have to manually enumerate several targets.

What I was searching for is a solution for the following situation:

- whenever I `make all` in the toplevel folder, `foo` and `bar/quux` shall not be built
- `foo` (and all targets within and below that folder) shall be built
  - when I `make all` in the `foo` folder
  - when I `make all` in the `bar/quux` folder

All targets within `foo` are already dependency of the (already existing) `foo/all` target (or artifact or whatever that `all` is). Similarly for `bar/quux/all`. As manual enumeration of the targets in parallel to the already existing `all` targets is error prone, I am searching for some easy way to have `bar/quux/all` depend on `foo/all` (or a semantically equivalent solution).

Kind regards

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 15, 2021, 1:45pm UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/8 "2021-10-15T13:45:32Z")

</div>

What you want is not possible and don’t think in terms of targets generated by `CMake`. They are internal and private to achieve correct build. Local `all` targets are for building targets included in `ALL`.

In your case, I suggest the creation of a custom target and, for each created target not in `ALL`, add a dependency to this custom target. So by launching this custom target you will achieve what you want.

```cmake
# in toplevel/foo
add_custom_target(foo)
add_executable(hello main.c)
add_dependency(foo hello)
# you can create a function to wrap these actions

# in toplevel/bar/quux
add_custom_target(quux)
# do same as in foo

# in top level
add_dependency(foo quux)

```

After `CMake` generation, `make foo` will build all targets in `foo`and dependencies.

---

<div class="post-metadata">

**Author:** ![ingolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@ingolf](https://discourse.cmake.org/u/ingolf)\
**Post date:** [October 15, 2021, 1:54pm UTC](https://discourse.cmake.org/t/register-dependencies-between-all-targets/4278/9 "2021-10-15T13:54:55Z")

</div>

Thanks a lot for your support, @marc.chevrier.

Cheers  
Ingolf
