Writing Custom Commands

Let's learn about the non-target mechanism to create custom artifacts that can be used in all targets.

Writing custom commands

There's also an interesting non-target mechanism to create custom artifacts that can be used in all actual targets: custom commands

Using custom targets has one drawback: as soon as we add them to the ALL target or start depending on them for other targets, they will be built every single time (we may still enable them in an if block to limit that). Sometimes, this is what we want, but there are cases when custom behavior is necessary to produce files that shouldn't be recreated without reason:

  • Generating a source code file that another target depends on

  • Translating another language into C++

  • Executing a custom action immediately before or after another target was built

There are two signatures for a custom command. The first one is an extended version of add_custom_target():

Get hands-on with 1200+ tech skills courses.