如何将git commit-number包含到c ++可执行文件中? [英] how to include git commit-number into a c++ executable?
问题描述
我使用git作为我的c ++项目的版本跟踪器。
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.
我能想到的一种方法是使c ++程序午餐git log从shell中提取提交#但我不知道如何在make过程中做到这一点。
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.
(我使用linux)
推荐答案
也许这样做会添加到您的Makefile的规则来产生与当前的git提交ID .c文件的最简单方法:
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)\";" > $@
现在只需添加 gitversion.c
到正常的构建过程。确保在 make clean
中删除它,并将它添加到 .gitignore
中,以免它偶然添加到git存储库。在某处添加一个 extern const char * gitversion;
标题,并且可以像这样访问它。
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.
这篇关于如何将git commit-number包含到c ++可执行文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!