有没有办法在ELF可执行文件中嵌入版本信息,例如git commit hash,以便可以从它生成的核心转储中检索它?
答案 0 :(得分:1)
请参阅this question获取git哈希值。
然后,更改您的构建过程(例如您的Makefile
)以包含它。例如,使用
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
选项传递给您的程序时)