This is the batch(GenerateHash.cmd) code I used to generate a hash source file, I used it for many years.
I used it as a pre-build steps in the Code::Blocks' project build options.
The source file(version.cpp) has some contents like below:
const char* GITHASH="971ec0e821a9dd008c785a9b53e030e939dfef00";
Then you can link the version.cpp in your project.
In the pre build step, I wrote
Here is the content of the batch file:
@echo off
REM a way to get the command line response from the git command
REM windows batch (bat, cmd) - How to set command output to variable https://gist.github.com/pavelburov/4dd76f12ef2d08c401bf54ec344efae9
for /f %%i in ('git rev-parse HEAD') do set VARIABLE=%%i
echo %VARIABLE%
REM https://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
REM some special characters should be escaped
If not exist version.cpp (
echo const char* GITHASH=^"%VARIABLE%^"^; > version.cpp
goto EndOfFile
)
echo const char* GITHASH=^"%VARIABLE%^"^; > temp.version.cpp
REM echo abc>a.txt
REM echo abc1>b.txt
REM compare the two files using the FC command
REM FC - file compare - Windows CMD - SS64.com https://ss64.com/nt/fc.html
FC version.cpp temp.version.cpp >NUL
IF %ERRORLEVEL% == 0 (
REM the two files are the same
echo "Same commit hash, no need to update version.cpp"
goto EndOfFile
) ELSE (
REM something different, update the version.h file with new hash
copy temp.version.cpp version.cpp
echo "New commit hash found, version.cpp updated"
)
:EndOfFile
When the hash value changes(I'm using a compare command "FC" to compare the hash strings), I need to update the version.cpp file.