Advanced Custom Commands
Introduction
Custom commands can be tied to specific build events, such as pre-build or post-build steps. This allows you to automate tasks like:
- Running a code formatter before building.
- Copying files or running tests after building.
- Generating files dynamically based on build configurations.
Key Concepts
PRE_BUILD
,PRE_LINK
, andPOST_BUILD
: These options tie custom commands to specific stages of the build process.- Generator Expressions: Special CMake syntax (e.g.,
$<CONFIG:Debug>
) that evaluates at build time. - Combining Commands: Use multiple
add_custom_command
calls to create complex workflows.
Code Sample
- We will start with the
main.cpp
file and theCMakeLists.txt
file fromChapter 1
.
- In addition to the standard lines for compilations, add a few lines to
CMakeLists.txt
to run a command after the build completes.
- Run
cmake
and build, and observe the output
- Output:
...
Running post-build step
Build succeeded!