Auto‑Embed Build Version Info in Embedded Software with Git and date
This guide explains how to automatically generate and embed a software version string during compilation for embedded projects, using a header file updated by a Bash script that leverages the Linux date command and Git metadata to simplify version tracking and debugging.
1. Software Management Challenges
Large‑scale software projects often have long cycles, many temporary builds, and numerous bugs caused by numerous contributors with varying skill levels, leading to version‑tracking problems such as missing test builds and performance‑impacting disputes.
2. Simplified Version Number
For clarity, the article proposes a minimal version identifier that only prints the release timestamp, illustrated by the following image:
3. Principle
A dedicated header file sw_version.h stores the version macro. The main program includes this header and prints the macro at startup. During compilation, a script mk.sh updates the header with the current date, so the built executable always carries the latest version information.
#define SW_VERSION "2024-09-09-7"The header can be updated either by a C program or directly via the date command; the article demonstrates the latter.
4. date Command Options
The Linux date utility can display or set system time. Common options used in this tutorial include: --help: show help --version: show version -u: display UTC time -d: perform date arithmetic
Key usage for this guide is displaying the current date/time in a custom format:
# date '+%Y-%m-%d %H:%M:%S'Result example (image):
5. Bash Script Example
#!/bin/bash
str_front=#define SW_VERSION
quotation="\""
version_file=sw_version.h
rm $version_file
echo $str_front $quotation$(date '+%Y-%m-%d-%H')$quotation >> $version_fileExplanation:
Define str_front to hold the macro prefix.
Define quotation to hold a double‑quote character.
Set version_file to the header name.
Remove any existing header.
Append a line like #define SW_VERSION "2024-09-09-7" generated by date to the header.
6. Result
Running the script produces a sw_version.h containing the updated version macro, which can be compiled into the application. The output screenshots (shown below) confirm the header content and the printed version at program start.
Developers can adapt the script and format to meet their own version‑numbering conventions and integrate it into existing build pipelines.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
