# Why can't this work? add\_custom\_command, add\_custom\_target?

**URL:** https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929
**Category:** Code
**Created:** [September 28, 2020, 5:08pm UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929 "2020-09-28T17:08:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![swoo.quek](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@swoo.quek](https://discourse.cmake.org/u/swoo.quek)
#### Post date: [September 28, 2020, 5:08pm UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929/1 "2020-09-28T17:08:43Z")

</div>

Can anyone tell me why the below does not work correctly?

I meant to do this:

If timer\_cx.h is changed, then timer\_cx.c should be recompiled and timer\_cx.c.obj should be generated  
Then, MY\_Project should be re-built, because timer\_cx.c.obj has changed

But when I execute “make all”:  
timer\_cx.c.obj is regenerated, but  
MY\_Project is not rebuilt, ie. MY\_Project.exe is not re-generated ← Why?

> add\_executable(MY\_Project ${SOURCE})

> set(IF\_f “${CMAKE\_CURRENT\_SOURCE\_DIR}/timer/timer\_cx.h”)

> add\_custom\_command(  
> OUTPUT “${CMAKE\_CURRENT\_BINARY\_DIR}/CMakeFiles/MY\_Project.dir/timer/src/timer\_cx.c.obj”  
> COMMAND ${CMAKE\_CXX\_COMPILER} -subcommand=${COMPFILE} -debug -nofpu -output=obj=${CMAKE\_CURRENT\_BINARY\_DIR}/CMakeFiles/MY\_Project.dir/timer/src/timer\_cx.c.obj ${CMAKE\_CURRENT\_SOURCE\_DIR}/timer/src/timer\_cx.c  
> DEPENDS ${IF\_f}  
> VERBATIM  
> )

> add\_custom\_target(  
> what ALL  
> DEPENDS “${CMAKE\_CURRENT\_BINARY\_DIR}/CMakeFiles/MY\_Project.dir/timer/src/timer\_cx.c.obj”  
> VERBATIM  
> )

---

<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: [September 28, 2020, 5:50pm UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929/2 "2020-09-28T17:50:07Z")

</div>

Why are you running the compiler manually in a custom command? What’s wrong with just adding `timer_cx.c` to the list of sources for your executable?

Either way, it doesn’t work because `MY_Project` has no idea that it is supposed to include the object file you’re creating here.

---

<div class="post-metadata">

### Author: ![swoo.quek](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@swoo.quek](https://discourse.cmake.org/u/swoo.quek)
#### Post date: [September 29, 2020, 2:46am UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929/3 "2020-09-29T02:46:02Z")

</div>

Dear Ben,

> > Either way, it doesn’t work because `MY_Project` has no idea that it is supposed to include the object file you’re creating here.

I also suspect the mistake is there, which means `MY_Project` is not ‘sensitive’ to changes in the object file

However, the custom target `what` has been added to `ALL` already:

> add\_custom\_target(  
> what ALL  
> DEPENDS “${CMAKE\_CURRENT\_BINARY\_DIR}/CMakeFiles/MY\_Project.dir/timer/src/timer\_cx.c.obj”  
> VERBATIM  
> )

Doesn’t the above establish the dependency between the custom target and my executable target?  
Unless my understanding of `ALL` target is wrong?

Or should it be like this:

> add\_custom\_target(  
> what **${CMAKE\_PROJECT\_NAME}**  
> DEPENDS “${CMAKE\_CURRENT\_BINARY\_DIR}/CMakeFiles/MY\_Project.dir/timer/src/timer\_cx.c.obj”  
> VERBATIM  
> )

---

<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: [September 29, 2020, 1:58pm UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929/4 "2020-09-29T13:58:14Z")

</div>

`ALL` just means the custom target is run in the default set of targets when running the build without a specific list of targets given. It has zero relation to any other target in the project.

Again, why is listing `timer_cx.c` as a source of your executable not sufficient?

---

<div class="post-metadata">

### Author: ![swoo.quek](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@swoo.quek](https://discourse.cmake.org/u/swoo.quek)
#### Post date: [September 29, 2020, 4:56pm UTC](https://discourse.cmake.org/t/why-cant-this-work-add-custom-command-add-custom-target/1929/5 "2020-09-29T16:56:42Z")

</div>

Dear Ben,

> > Again, why is listing `timer_cx.c` as a source of your executable not sufficient?

timer\_cx.c in fact is listed as a source of the executable already, ${SOURCE} actually includes all \*.c file

> Either way, it doesn’t work because `MY_Project` has no idea that it is supposed to include the object file you’re creating here.

I managed to get it to work based on your comment above! I added `add_dependencies`:

> add\_custom\_command(OUTPUT …)  
> add\_custom\_target(header-target …)  
> **add\_dependencies** (${CMAKE\_PROJECT\_NAME} header-target)

Thanks for the clue!
