# How to add dependencies to an object library \[for ninja generator\]?

**URL:** https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462
**Category:** Usage
**Tags:** gen:ninja
**Created:** [February 14, 2023, 3:44am UTC](https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462 "2023-02-14T03:44:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![LittleFall](https://discourse.cmake.org/user_avatar/discourse.cmake.org/littlefall/32/3166_2.png) [@LittleFall](https://discourse.cmake.org/u/LittleFall)
#### Post date: [February 14, 2023, 3:44am UTC](https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462/1 "2023-02-14T03:44:54Z")

</div>

The full source and cmake files can be found in this issue.  
[Ninja: Cannot create dependency of an object library (#24411) · Issues · CMake / CMake · GitLab (kitware.com)](https://gitlab.kitware.com/cmake/cmake/-/issues/24411)

```cmake
add_library(main OBJECT main.cpp)
target_link_libraries(main a)
add_dependencies(main a)

```

Neither target\_link\_libraries(main a) nor add\_dependencies(main a) can request compilation `a` when compiling `main`.

1. How do I create dependencies for the object library in the ninja generator?
2. The ninja generator does this and the makefile generator does not, is this expected?

---

<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: [February 14, 2023, 3:59am UTC](https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462/2 "2023-02-14T03:59:15Z")

</div>

What is the use case for having `a`’s objects compile when requesting `main`? Nothing actually requires them to be there (as there is no linking step).

---

<div class="post-metadata">

### Author: ![LittleFall](https://discourse.cmake.org/user_avatar/discourse.cmake.org/littlefall/32/3166_2.png) [@LittleFall](https://discourse.cmake.org/u/LittleFall)
#### Post date: [February 14, 2023, 5:23am UTC](https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462/3 "2023-02-14T05:23:43Z")

</div>

Thanks, I was just about to ask the cmake experts for help with my needs.

My original requirement is that I want to add a lightweight check target to my c++ repository: compile only, not link, or even use -fsyntax-only at compile time, without generating targets. (similar to rust’s cargo check)

Suppose the original final target is main, and main depends on a and b; a depends on c and d.

Consider writing a macro or function create\_shadow\_target(X)

It does the following:

1. create a target named X\_shadow, type is object library
2. target\_compile\_options(X\_shadow PRIVATE -fsyntax-only)
3. The direct source of X\_shadow is the same as X.
4. for each library A linked by X.  
a. Perform this procedure recursively, generating A\_shadow;  
b. Force a dependency between X\_shadow and A\_shadow  
c. Make X\_shadow obtain A\_shadow’s headers, and public dependencies  
target\_include\_directories(X\_shadow PRIVATE $\<TARGET\_PROPERTY:A\_shadow,INTERFACE\_INCLUDE\_DIRECTORIES\>)

Then just generate the main\_shadow target and run it.

now I can’t do `4.b` because I can’t add dependencies to the object library.

and do you have any suggestions for other parts?

---

<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: [February 14, 2023, 4:00pm UTC](https://discourse.cmake.org/t/how-to-add-dependencies-to-an-object-library-for-ninja-generator/7462/4 "2023-02-14T16:00:25Z")

</div>

I think for `4.c`, you can use `$<TARGET_PROPERTY:X,INCLUDE_DIRECTORIES>` and get the transitive closure automatically and not need to do the shallow traversal yourself.

For `4.b`, I think, in general, you want to depend on `A` directly because you may need a generated header that is attached to a custom command there. Which is actually the case for `X` as well, so you actually want `X_shadow` to depend on `X` here. If you know you don’t have such things, can you instead just make a purely shadow graph (with no links to the normal graph) and wrap it up with a `shadow` custom target that depends on all of them? In that case, I don’t think you need the intra-shadow dependencies either for that matter.

> [@LittleFall](#):
>
> now I can’t do `4.b` because I can’t add dependencies to the object library.

You can add dependencies, but any link requirements cannot be drawn because there is no “link” node to draw them to (there is an overall target node, but I think we didn’t give it link semantics explicitly).
