有没有一种方法可以将版本信息(如git commit hash)embedded到ELF可执行文件中,从而可以从其生成的核心转储中检索它?
看到这个问题得到git散列。
然后,改变你的构建过程(例如你的Makefile
)来包含它。 例如,生成一行C文件
git log --pretty=format:'const char program_git_hash[] = "%H";' \ -n 1 > _prog_hash.c
然后用_prog_hash.c
链接你的程序,并在链接后删除该文件。 你可以添加时间戳信息例如
date +'const char program_timestamp="%c";%n' >> _prog_hash.c
然后你可以在二进制可执行文件上使用gdb
或strings
来查找它。
你可能想声明extern const char program_git_hash[];
在一些头文件中,也许显示它(例如,当传递--version
选项到你的程序)