how to include git commit-number into a c++ executable?

前端 未结 4 1409
离开以前
离开以前 2020-12-22 22:29

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.

wh

4条回答
  •  时光取名叫无心
    2020-12-22 22:52

    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:

    gitversion.c: .git/HEAD .git/index
        echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@
    

    Now simply add gitversion.c to your build process as normal. Make sure to remove it on make clean, and add it to .gitignore so it's not added to the git repository accidentally. Add an extern const char *gitversion; to a header somewhere, and you can access it like that.

提交回复
热议问题