Getting ready

We will need two source files, both very similar to the previous recipe. One will be configured with the recorded hash (version.hpp.in), as follows:

#pragma once  #include <string>const std::string GIT_HASH = "@GIT_HASH@";

We will also need an example source file (example.cpp), which will print the hash to the screen:

#include "version.hpp"#include <iostream>int main() {  std::cout << "This code has been configured from version " << GIT_HASH            << std::endl;}

This recipe also assumes that we are in a Git repository with at least one commit. So, initialize this example with git init, and create commits with git add <filename>, followed by git commit, in order to get a meaningful example.

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.