我有一个用C++编写的大小合理(约40k行)的机器学习系统。这仍处于积极开发中,即使我更改了代码,也需要定期运行实验。
我的实验输出记录在简单的文本文件中。在查看这些结果时,我想做的是通过某种方式找出产生它的代码的确切版本。我通常有大约5到6个实验同时运行,每个实验的代码版本略有不同。
我想知道,例如,通过编译文件A的版本1,文件B的版本2等获得了一组结果(我只需要一些标识符,“git describe”的输出在这里就可以了)。
我的想法是在编译二进制文件时以某种方式包括此信息。这样,可以将其与结果一起打印出来。
任何建议如何以一种不错的方式完成。特别是,用git做到这一点的任何好方法吗?
最佳答案
作为构建过程的一部分,我生成一个源文件,如下所示:
static const char version_cstr[] = "93f794f674 (" __DATE__ ")";
const char * version()
{
return version_cstr;
}
然后很容易在启动时注销版本。
我最初在命令行上使用了DEFINE,但这意味着每个版本都会更改,所有内容都将由构建系统重新编译-对于大型项目而言并不理想。
这是我用来生成
scons
的片段,也许您可以根据需要进行调整。# Lets get the version from git
# first get the base version
git_sha = subprocess.Popen(["git","rev-parse","--short=10","HEAD"], stdout=subprocess.PIPE ).communicate()[0].strip()
p1 = subprocess.Popen(["git", "status"], stdout=subprocess.PIPE )
p2 = subprocess.Popen(["grep", "Changed but not updated\\|Changes to be committed"], stdin=p1.stdout,stdout=subprocess.PIPE)
result = p2.communicate()[0].strip()
if result!="":
git_sha += "[MOD]"
print "Building version %s"%git_sha
def version_action(target,source,env):
"""
Generate file with current version info
"""
fd=open(target[0].path,'w')
fd.write( "static const char version_cstr[] = \"%s (\" __DATE__ \")\";\nconst char * version()\n{\n return version_cstr;\n}\n" % git_sha )
fd.close()
return 0
build_version = env.Command( 'src/autogen/version.cpp', [], Action(version_action) )
env.AlwaysBuild(build_version)
关于c++ - 跟踪可执行文件中的代码版本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7327845/