Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a version number in a static library?

How can I store a version number in a static library (file.a) and later check for its version in Linux?

P.S. I need possibility to check version of file any time without any special executable using only by shell utilities.

like image 227
Pirks Avatar asked Oct 28 '09 15:10

Pirks


People also ask

What is the difference between static library and DLL?

What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Is .LIB a static library?

Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, . a files in Linux and . lib files in Windows.

Do static libraries have dependencies?

A program linked against a static library has all dependencies self-contained in the binary file: this is called compile-time linking. The operating system reads instructions and data from the executable and copies them into memory with no modification, ready to be processed by the CPU.

What is the format of a static library?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.


2 Answers

Maybe you could create a string with the version like this:

char* library_version = { "Version: 1.3.6" };

and to be able to check it from the shell just use:

strings library.a | grep Version | cut -d " " -f 2
like image 131
Puppe Avatar answered Oct 13 '22 01:10

Puppe


In addition to providing a static string as mentioned by Puppe, it is common practice to provide a macro to retrieve the version check for compatibility. For example, you could have the following macros (declared in a header file to be used with your library):

#define MYLIB_MAJOR_VERSION 1
#define MYLIB_MINOR_VERSION 2
#define MYLIB_REVISION 3
#define MYLIB_VERSION "1.2.3"
#define MYLIB_VERSION_CHECK(maj, min) ((maj==MYLIB_MAJOR_VERSION) && (min<=MYLIB_MINOR_VERSION))

Notice with the MYLIB_CHECK_VERSION macro, I'm assuming you want a specific major rev and a minor rev greater than or equal to your desired version. Change as required for your application.

Then use it from a calling application, something like:

if (! MYLIB_VERSION_CHECK(1, 2)) {
    fprintf(stderr, "ERROR: incompatible library version\n");
    exit(-1);
}

This approach will cause the version information to come from the included header file. Additionally, it will be optimized at compile time for the calling application. With a little more work, you can extract it from the library itself. Read on...

You can also use this information to create a static string stored inside your library, as mentioned by Puppe. Place something like this inside your library:

struct {
    const char* string;
    const unsigned major;
    const unsigned minor;
    const unsigned revision;
} mylib_version = {
    MYLIB_VERSION, MYLIB_MAJOR_VERSION, MYLIB_MINOR_VERSION, MYLIB_REVISION
};

This will create a struct called mylib_version in your library. You can use this to do further verifications by creating functions inside your library and accessing those from a calling application, etc.

like image 30
jheddings Avatar answered Oct 13 '22 01:10

jheddings