# Library target isn't rebuilt when it depends on another custom target.

**URL:** https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311
**Category:** Code
**Created:** [December 3, 2019, 3:37am UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311 "2019-12-03T03:37:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ginee](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@ginee](https://discourse.cmake.org/u/ginee)
#### Post date: [December 3, 2019, 3:37am UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311/1 "2019-12-03T03:37:10Z")

</div>

I made a dependency on a library target for a custom target by  
“ADD\_DEPENDENCIES(lib\_target extern\_target)”.  
It works well that every time lib\_target is built, extern\_target will be built.  
However, when associated files in extern\_taget is updated, it doesn’t introduce a rebuilt on lib\_target.

```
SET(GENERATED_IMAGE ${CMAKE_BINARY_DIR}/test.bin)

ADD_CUSTOM_COMMAND(
    OUTPUT
        ${GENERATED_IMAGE}
    COMMAND
        CROSS_COMPILE=${CROSS_COMPILE}-...
        .....
     DEPENDS
        crm/aaa.c
        crm/bbb.h
        crm/ddd.c
        ...
     )

    ADD_CUSTOM_TARGET(extern_target DEPENDS ${GENERATED_IMAGE})
    
    ADD_LIBRARY(lib_target STATIC "")
    TARGET_SOURCES(lib_target 
    PRIVATE
        xxx.c
        yyy.c
 )

ADD_DEPENDENCIES(lib_target extern_target)

```

When aaa.c is changed, custom command is executed and bin file is rebuilt.  
But I suppose lib\_target should be rebuilt as well.

How can I add the dependency?  
Thanks.

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [December 3, 2019, 9:12am UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311/2 "2019-12-03T09:12:52Z")

</div>

Yes, this is a strange “feature” I also recently came along.

If you read the documentation on [add\_dependencies](https://cmake.org/cmake/help/latest/command/add_dependencies.html) carefully, you can see that it only states:

> Makes a top-level `<target>` depend on other top-level targets to ensure that they build before `<target>` does

**And not** : “If a rebuilds, b will also rebuild.”

I don’t know any good solution to your specific problem. I would touch all source files of `lib_target` in your custom command via generator expression.

---

<div class="post-metadata">

### Author: ![ginee](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@ginee](https://discourse.cmake.org/u/ginee)
#### Post date: [December 3, 2019, 9:25am UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311/3 "2019-12-03T09:25:05Z")

</div>

Yap, it looks like add\_dependencies() just sets a prerequisite of the target.  
Thanks for the suggestion and it is what I am doing right now to touch source files.  
Just wondering if any other solution here.

---

<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: [December 3, 2019, 4:09pm UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311/4 "2019-12-03T16:09:24Z")

</div>

> [@ginee](#):
>
> Just wondering if any other solution here.

If `${GENERATED_IMAGE}` is added to a source in `lib_target` in some way, that should do it. I don’t think there’s a way to do what it appears you’re looking for (cross compile something and then embed that into a static library).

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [December 10, 2019, 7:55am UTC](https://discourse.cmake.org/t/library-target-isnt-rebuilt-when-it-depends-on-another-custom-target/311/5 "2019-12-10T07:55:44Z")

</div>

Maybe ben is saying the same as me (I dont understand him, sorry) but a (nice?) solution would be:

```auto
ADD_CUSTOM_COMMAND(
    OUTPUT
        ${GENERATED_IMAGE}
       generated.cpp
    COMMAND
        CROSS_COMPILE=${CROSS_COMPILE}-...
        .....
        touch generated.cpp
     DEPENDS
        crm/aaa.c
        crm/bbb.h
        crm/ddd.c
        ...
     )
    
    ADD_LIBRARY(lib_target STATIC "")
    TARGET_SOURCES(lib_target 
    PRIVATE
        xxx.c
        yyy.c
       generated.cpp
 )

```
