Getting ready

We will use the following C file (example.c) to print the version information:

#include "version.h"#include <stdio.h>int main() {  printf("This is output from code %s\n", PROJECT_VERSION);  printf("Major version number: %i\n", PROJECT_VERSION_MAJOR);  printf("Minor version number: %i\n", PROJECT_VERSION_MINOR);  printf("Hello CMake world!\n");}

Here, we assume that PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, and PROJECT_VERSION are defined in version.h. Our goal is to generate version.h from the following scaffold, version.h.in:

#pragma once#define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@#define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@#define PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@#define PROJECT_VERSION "v@PROJECT_VERSION@" ...

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.