When invoking the CMake project command with a VERSION argument, CMake will set the PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, and PROJECT_VERSION_PATCH for our project. The key command in this recipe is configure_file, which takes an input file (in this case, version.h.in) and generates an output file (in this case, generated/version.h) by expanding all placeholders between @ to their corresponding CMake variables. It replaces @PROJECT_VERSION_MAJOR@ with 2, and so on. With the keyword @ONLY, we limit configure_file to only expand @variables@, but to not touch ${variables}. The latter form is not used in version.h.in, but they frequently appear when configuring a shell script using CMake.
The generated header file can be included in our example code, and the version information is available to be printed:
$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
$ ./example
This is output from code v2.0.1
Major version number: 2
Minor version number: 0
Hello CMake world!