A practical guide to embedding Git repository state (commit hash, tag, branch) into C++ code at compile time using CMake. The approach uses a standalone version.cmake script that runs git commands, generates a version.cpp file with string constants, and integrates with CMakeLists.txt via a custom command that forces re-execution before every build. The solution is fast: ~0.09s when nothing changed, ~0.28s when regeneration and relinking are needed.

3m read timeFrom mattkeeter.com
Post cover image

Sort: