i use git as a version tracker for my c++ project.
sometimes i need to repeat a calculation and i would like to know which version of the program i used.
what would be a good way to put a # of the commit into the main executable? in other words. i'd like the program to tell me the # of the current commit in an introductory message as i run the program.
one way i can think of is to make the c++ program lunch "git log" from shell and extract the commit # but i'm not sure how to do it during make.
(I use linux)
If you're using Qt, put this in your project's .pro file:
Then use
GIT_REVISION
in your code as in the other answers - it behaves asconst char *
.(Thanks to Alexander Barthel, who I plundered this tip from.)
I do the following in CMakeLists.txt:
So the
git rev-parse --short HEAD
's output is good to build in the binary.Probably the easiest way to do this would be to add to your makefile a rule to generate a .c file with the current git commit ID:
Now simply add
gitversion.c
to your build process as normal. Make sure to remove it onmake clean
, and add it to.gitignore
so it's not added to the git repository accidentally. Add anextern const char *gitversion;
to a header somewhere, and you can access it like that.I use
git describe
to get a version which either uses a tag or commit number. This usually gives nice versions like:v0.1-1-g787c667
if the tip of the branch has additional commits above the 'v0.1' tag.The git command I use is:
git describe --tags --always
. I usually use it with the SCons build system and define it as a constant, relevant parts of the SConstruct:In the C or C++ program I can now access
GIT_DESC
as a string-constant:note: the
--abbrev=N
argument togit describe
might be useful to achieve consistent version output independent of a users git configuration.