<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte" -->

---
title: Compile-time version strings in CMake | daily.dev
description: 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...
canonical: https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Compile-time version strings in CMake | daily.dev
og:description: 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...
og:url: https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte
og:image: https://api.daily.dev/og/posts/D9g9AyltE.png
og:image:alt: Compile-time version strings in CMake
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Compile-time version strings in CMake

**[Matt Keeter](https://daily.dev/sources/mattkeeter)** · 3 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://mattkeeter.com/blog/2018-01-06-versioning>

## Similar posts on daily.dev

- [Stamp It\! All Programs Must Report Their Version](https://daily.dev/posts/stamp-it-all-programs-must-report-their-version-cyblih53k) · Lobsters · 0 upvotes · 0 comments
- [I made my own git](https://daily.dev/posts/i-made-my-own-git-fwqs5dx1b) · Hacker News · 77 upvotes · 4 comments
- [Who Built This?](https://daily.dev/posts/who-built-this--bc4rx6op2) · Andrew Nesbitt · 0 upvotes · 0 comments
- [Linus wrote Git in about 10 days, so I asked my local LLM to build something similar with one prompt](https://daily.dev/posts/linus-wrote-git-in-about-10-days-so-i-asked-my-local-llm-to-build-something-similar-with-one-prompt-c0eheydp4) · XDA Developers · 1 upvotes · 0 comments
- [I Built Git from Scratch to Finally Understand What I've Been Using for Years](https://daily.dev/posts/i-built-git-from-scratch-to-finally-understand-what-i-ve-been-using-for-years-3wx3ymzn1) · DEV · 23 upvotes · 0 comments

---

Tags: [#c](https://daily.dev/tags/c), [#git](https://daily.dev/tags/git), [#build-systems](https://daily.dev/tags/build-systems)

[View this post on daily.dev](https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Compile-time version strings in CMake","url":"https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte"},"datePublished":"2026-03-31T12:30:33.980Z","dateModified":"2026-03-31T12:58:11.130Z","description":"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...","image":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","thumbnailUrl":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","isAccessibleForFree":true,"articleSection":"Matt Keeter","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Matt Keeter","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/mattkeeter"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/compile-time-version-strings-in-cmake-d9g9aylte","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"c,git,build-systems","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Matt Keeter","item":"https://daily.dev/sources/mattkeeter"},{"@type":"ListItem","position":3,"name":"Compile-time version strings in CMake"}]}
```

