Is there a way to embed version info such as a git commit hash in an ELF executable such that it can be retrieved from core dumps generated from it?

有帮助吗?

解决方案

See this question to get the git hash.

Then, change your build procedure (e.g. your Makefile) to include it. For instance, generate a one line C file with

  git log --pretty=format:'const char program_git_hash[] = "%H";' \
      -n 1 > _prog_hash.c

then link your program with _prog_hash.c and remove that file after linking. You could add timestamping information with e.g.

 date +'const char program_timestamp="%c";%n' >> _prog_hash.c

Then you could use gdb or strings on the binary executable to find it.

You may want to declare extern const char program_git_hash[]; in some header file, and perhaps display it (e.g. when passing the --version option to your program)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top