Pavel Davydov
Pavel Davydov

Reputation: 3569

How to check shared library version in the binary

I have a program that is linked with some shared library on start. There are multiple versions of the library installed on the host. Is there some function or macro that can check the version of library that was linked to my program? I want something like this:

int main() {
    REQUIRE_LIBRARY_VERSION_GREATER("libgcc", 1, 2, 3); //example
}

Is it possible in unix? I need it at least on linux and freebsd.

EDIT: I would prefer to avoid fork/exec if possible.

Upvotes: 0

Views: 4767

Answers (2)

Dabo
Dabo

Reputation: 2373

I use dl_iterate_phdr function to find out which shared objects are loaded. This is example of dl handler that prints shared libs info

static int header_handler(struct dl_phdr_info* info, size_t size, void* data)
{
    printf("name=%s (%d segments) address=%p\n",
            info->dlpi_name, info->dlpi_phnum, (void*)info->dlpi_addr);
    for (int j = 0; j < info->dlpi_phnum; j++) {
         printf("\t\t header %2d: address=%10p\n", j,
             (void*) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
         printf("\t\t\t type=%u, flags=0x%X\n",
                 info->dlpi_phdr[j].p_type, info->dlpi_phdr[j].p_flags);
    }
    printf("\n");
    return 0;
}

It is taken from here

It prints for my video processing project following :

name=/usr/local/lib/libopencv_highgui.so.2.4 (7 segments) address=0x7f467935a000
name=/usr/local/lib/libopencv_imgproc.so.2.4 (7 segments) address=0x7f46796e9000
...

Opencv includes the version in the name of library libopencv_imgproc.so*.2.4* . This way i know wich opencv version loaded. I don't know if it is good for your libraries, but it can be a starting point for your.

Upvotes: 1

gino pilotino
gino pilotino

Reputation: 860

The ldd command list shared libraries for your executable

ldd <executable>

Upvotes: 1

Related Questions