How to embed version information into shared library and binary?

前端 未结 4 494
难免孤独
难免孤独 2020-12-17 21:06

On Linux, is there a way to embed version information into an ELF binary? I would like to embed this info at compile time so it can then be extract it using a script later.

4条回答
  •  半阙折子戏
    2020-12-17 21:27

    To extend the @sashang answer, while avoiding the "$Id:$" issues mentioned by @cdunn2001, ...

    You can add a file "version_info.h" to your project that has only:

    #define VERSION_MAJOR "1"
    #define VERSION_MINOR "0"
    #define VERSION_PATCH "0"
    #define VERSION_BUILD "0"
    

    And in your main.c file have the line:

    static char version[] = VERSION_MAJOR "." VERSION_MINOR "." VERSION_PATCH "." VERSION_BUILD;
    static char timestamp[] = __DATE__ " " __TIME__;
    

    (or however you want to use these values in your program)

    Then set up a pre-build step which reads the version_info.h file, bumps the numbers appropriately, and writes it back out again. A daily build would just bump the VERSION_BUILD number, while a more serious release would bump other numbers.

    If your makefile lists this on your object's prerequisite list, then the build will recompile what it needs to.

提交回复
热议问题