Tutorial :Linux: How to embed version information into shared library and binary?



Question:

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. A hackish way would be to plant something that can be extracted using the strings command. Is there a more conventional method, similar to how Visual Studio plant version info for Windows DLLs (note version tab in DLL properties)?


Solution:1

One way to do it if using cvs or subversion is to have a special id string formatted specially in your source file. Then add a pre-commit hook to cvs or svn that updates that special variable with the new version of the file when a change is committed. Then, when the binary is built, you can use ident to extract that indformation. For example:

Add something like this to your cpp file:

static char fileid[] = "$Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $";  

And running ident (which you can find by installing rcs) on the program should show the info about the files that have an id string in them.

ident program  program:      $Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $  

Note As people have mentioned in the comments this technique is archaic. Having the source control system automatically change your source code is ugly and the fact that source control has improved since the days when cvs was the only option means that you can find a better way to achieve the same goals.


Solution:2

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.


Solution:3

If you declare a variable called program_version or similar you can find out at which address the variable is stored at and then proceed to extract its value. E.g.

objdump -t --demangle /tmp/libtest.so | grep program_version  0000000000600a24 g     O .data  0000000000000004              program_version  

tells me that program_version resides at address 0000000000600a24 and is of size 4. Then just read the value at that address in the file.

Or you could just write a simple program that links the library in questions and prints the version, defined either as an exported variable or a function.


Solution:4

The Intel Fortran and C++ compilers can certainly do this, use the -sox option. So, yes there is a way. I don't know of any widespread convention for embedding such information in a binary and I generally use Emacs in hexl-mode for reading the embedded information, which is quite hackish.

'-sox' also embeds the compiler options used to build an executable, which is very useful.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »