# How to add custom command after compilation stage and before linking stage?

**URL:** <https://discourse.cmake.org/t/how-to-add-custom-command-after-compilation-stage-and-before-linking-stage/2033>\
**Category:** Code\
**Created:** [October 20, 2020, 8:07am UTC](https://discourse.cmake.org/t/how-to-add-custom-command-after-compilation-stage-and-before-linking-stage/2033 "2020-10-20T08:07:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [October 20, 2020, 8:07am UTC](https://discourse.cmake.org/t/how-to-add-custom-command-after-compilation-stage-and-before-linking-stage/2033/1 "2020-10-20T08:07:02Z")

</div>

Hi,  
I’m a writer of a code generator tool, and the tool must be run after the compilation stage and before linking stage.

We use makefile before and this is not a problem, but when ports to cmake, I only find adding flags to compilers and linkers, which seem not fit my requirement, so is there anyway I can do this?

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [October 20, 2020, 8:55am UTC](https://discourse.cmake.org/t/how-to-add-custom-command-after-compilation-stage-and-before-linking-stage/2033/2 "2020-10-20T08:55:53Z")

</div>

I believe you’re looking for the [“Build events” signature of `add_custom_command`](https://cmake.org/cmake/help/latest/command/add_custom_command.html#build-events):

```
add_custom_command(
  TARGET TheTarget 
  PRE_LINK
  COMMAND GeneratorTool arg1 arg2
  VERBATIM
  # add other arguments to taste
)

```

This adds a custom command that will run before `TheTarget` is linked, but after its sources have been compiled.

---

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [October 20, 2020, 8:58am UTC](https://discourse.cmake.org/t/how-to-add-custom-command-after-compilation-stage-and-before-linking-stage/2033/3 "2020-10-20T08:58:20Z")

</div>

Thanks a lot, seems I’m not carefully enough to read the document…
