How to do it

We will follow these steps to register the version in our template header file:

  1. To track the code version, we can define the project version when invoking the CMake project command in CMakeLists.txt:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-04 VERSION 2.0.1 LANGUAGES C)
  1. We then configure version.h, based on version.h.in:
configure_file(  version.h.in  generated/version.h  @ONLY  )
  1. Finally, we define the executable and provide the target include path:

add_executable(example example.c)target_include_directories(example  PRIVATE    ${CMAKE_CURRENT_BINARY_DIR}/generated  )

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.