Skip to content

Adding a Version Number from a Configured Header File

Felipe Torrezan edited this page Mar 23, 2024 · 4 revisions

Introduction

This interactive example describes how to consume the project()'s version in your source code. This example will print out the project version.

Helpful Resources

Tutorial

A CMake project example is provided at examples/version:

Project files
CMakeLists.txt
version.h.in
main.c

The main() function will print out the project version taken from version.h, provided by CMake at configure-time.

Tasks

  • Perform the following tasks on CMakeLists.txt (click to show/hide answers):
TODO 1: Add the project()'s VERSION and set it to 2.7.1
project(Project LANGUAGES C VERSION 2.7.1)
TODO 2: Configure version.h.in to generate version.h
configure_file(version.h.in version.h)
TODO 3: Add the Project Binary Directory to the target's include directories
target_include_directories(version PUBLIC "${PROJECT_BINARY_DIR}")
  • Perform the following task on version.h.in (click to show/hide answers):
TODO 4: Define the C macros with the CMake placeholders for version
#define Project_VERSION_MAJOR @Project_VERSION_MAJOR@
#define Project_VERSION_MINOR @Project_VERSION_MINOR@
#define Project_VERSION_PATCH @Project_VERSION_PATCH@
  • Finally build and test the project. Refer to the tutorial for more information.
Clone this wiki locally