# LINK\_DEPENDS on output of add\_custom\_command, no rule to make target

**URL:** <https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179>\
**Category:** Code\
**Created:** [February 24, 2024, 1:28pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179 "2024-02-24T13:28:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![risint96](https://discourse.cmake.org/user_avatar/discourse.cmake.org/risint96/32/4339_2.png) [@risint96](https://discourse.cmake.org/u/risint96)\
**Post date:** [February 24, 2024, 1:28pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/1 "2024-02-24T13:28:34Z")

</div>

Hi, I have a project that lives in a subdirectory `example` of another project, can’t figure out why it keeps on complaining that:

> No rule to make target ‘example/linkerscript.ld’, needed by ‘example/example’. Stop.

Here’s the code

```auto
cmake_minimum_required(VERSION 3.8)

project("example" LANGUAGES C)

add_executable(example main.c)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/generate_linkerscript.sh
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generate_linkerscript.sh ${CMAKE_LINKER} ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld
    COMMENT Generates linkerscript with custom sections
)
add_custom_target(linkerscript DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)

set_target_properties(example PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)
target_link_options(example PRIVATE -T${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)

```

Here’s the tree of the directory:

```auto
├── example
│ ├── CMakeLists.txt
│ ├── generate_linkerscript.sh
│ └── main.c

```

If I manually build the target `linkerscript`, then building `example` succeeds, however if I clean and just try building `example` I’m getting the error from the beginning.

This should have been pretty simple but it’s really frustrating.

Any help would be welcome!

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [February 24, 2024, 9:18pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/2 "2024-02-24T21:18:32Z")

</div>

Your `example` executable target has no dependency on the `${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld` or the `linkerscript` target. Adding the following line should fix your problem:

```cmake
add_dependencies(example linkerscript)

```

---

<div class="post-metadata">

**Author:** ![risint96](https://discourse.cmake.org/user_avatar/discourse.cmake.org/risint96/32/4339_2.png) [@risint96](https://discourse.cmake.org/u/risint96)\
**Post date:** [February 25, 2024, 8:28am UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/3 "2024-02-25T08:28:13Z")

</div>

I see, can you please explain why the following line wasn’t enough:

```auto
set_target_properties(example PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)

```

Thanks

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [February 25, 2024, 9:14pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/4 "2024-02-25T21:14:48Z")

</div>

I missed that you were setting the `LINK_DEPENDS` property. I see why you’re confused, that appears like it should have worked. Can you confirm you’re using one of the Makefiles generators or Ninja?

---

<div class="post-metadata">

**Author:** ![risint96](https://discourse.cmake.org/user_avatar/discourse.cmake.org/risint96/32/4339_2.png) [@risint96](https://discourse.cmake.org/u/risint96)\
**Post date:** [February 26, 2024, 7:24pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/5 "2024-02-26T19:24:41Z")

</div>

Using `Unix Makefiles` I’m getting this behavior.

However using `Ninja` it seems to build correctly, interesting.

Adding

```auto
add_dependencies(example linkerscript)

```

Resolves the issue for Makefiles however.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [February 26, 2024, 8:25pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/6 "2024-02-26T20:25:29Z")

</div>

This seems like a regression then. Could you please [open a bug report](https://gitlab.kitware.com/cmake/cmake/-/issues/new) to track it.

---

<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 27, 2024, 2:18pm UTC](https://discourse.cmake.org/t/link-depends-on-output-of-add-custom-command-no-rule-to-make-target/10179/7 "2024-02-27T14:18:34Z")

</div>

This is probably due to `Ninja` generators having a global graph where everything is visible. The `Makefiles` generators have some “fences” that hide rules in one subgraph from other subgraphs.
