Linux: how to embed version information in a shared library and binary?

On Linux, is there a way to include version information in an ELF binary? I would like to include this information at compile time so that it can later be retrieved using the script later. A hacky way would be to install something that can be retrieved with the strings command. Is there a more ordinary method, similar to how installation version information for Studio Studio for Windows DLL (tab with version note in DLL properties)?

+7
gcc linux shared-libraries
source share
4 answers

One way to do this, if you use cvs or subversion, is to use a special identifier string formatted specifically in the source file. Then add the pre-commit binding to cvs or svn, which updates this special variable with the new version of the file when the change is made. Then, when the binary is built, you can use the identifier to extract this information. For example:

Add something like this to the cpp file:

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

And the running identifier (which you can find by setting rcs) in the program should show information about files that have an id string.

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

Note As mentioned in the comments, this method is archaic. If the source control system automatically changes your source code, this is ugly, and the fact that source control has improved since cvs was the only option means you can find a better way to achieve the same goals.

+9
source share

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

You can add the file "version_info.h" to your project, which only has:

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

And in your main.c file there is a line:

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

(or, nevertheless, you want to use these values ​​in your program)

Then configure the pre-build step, which reads the version_info.h file, issues the numbers accordingly and writes it again. A daily build would simply increase the number of VERSION_BUILD, while a more serious release would affect other numbers.

If your make file lists this in the list of necessary objects, then the assembly will recompile what it needs.

+3
source share

Intel Fortran and C ++ compilers can certainly do this, use the -sox . So yes, there is a way. I don’t know of any widespread convention for embedding such information in a binary file, and I usually use Emacs in hex mode to read embedded information, which is pretty hacky.

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

+1
source share

If you declare a variable with the name program_version or similar, you can find out at what address the variable is stored, and then proceed to extract its value. For example.

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

tells me that program_version is located at 0000000000600a24 and has a size of 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 a version defined either as an exported variable or a function.

+1
source share

All Articles