Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm aware that shared objects under Linux use "so numbers", namely that different versions of a shared object are given different extensions, for example:

  • example.so.1
  • example.so.2

I understand the idea is to have two distinct files such that two versions of a library can exist on a system (as opposed to "DLL Hell" on Windows). I'd like to know how this works in practice? Often, I see that example.so is in fact a symbolic link to example.so.2 where .2 is the latest version. How then does an application depending on an older version of example.so identify it correctly? Are there any rules as to what numbers one must use? Or is this simply convention? Is it the case that, unlike in Windows where software binaries are transferred between systems, if a system has a newer version of a shared object it is linked to the older version automatically when compiling from source?

I suspect this is related to ldconfig but I'm not sure how.

share|improve this question

3 Answers

up vote 18 down vote accepted

Binaries themselves know which version of a shared library they depend on, and request it specifically. You can use ldd to show the dependencies; mine for ls are:

> ldd =ls
    linux-gate.so.1 =>  (0xb784e000)
    librt.so.1 => /lib/librt.so.1 (0xb782c000)
    libacl.so.1 => /lib/libacl.so.1 (0xb7824000)
    libc.so.6 => /lib/libc.so.6 (0xb76dc000)
    libpthread.so.0 => /lib/libpthread.so.0 (0xb76c3000)
    /lib/ld-linux.so.2 (0xb784f000)
    libattr.so.1 => /lib/libattr.so.1 (0xb76bd000)

As you can see, it points to e.g. libpthread.so.0, not just libpthread.so.


The reason for the symbolic link is for the linker. When you want to link against libpthread.so directly, you give gcc the flag -lpthread, and it adds on the lib prefix and .so suffix automatically. You can't tell it to add on the .so.0 suffix, so the symbolic link points to the newest version of the lib to faciliate that

share|improve this answer
Ahhh ok. I see. Thanks very much! – Antony Vennard Aug 13 '10 at 21:28

The numbers in the shared libraries are convention used in Linux to identify the API of a library. Typically the format is:

libFOO.MAJOR.MINOR.SO

And as you noticed usually there is a symbolic link from libFOO.so to libFOO.MAJOR.MINOR.so

The MAJOR is typically incremented when the API changes (new entry points are removed or the parameters or types changed). The MINOR typically is incremented for bug fix releases or when new APIs are introduced without breaking existing APIs.

The ldconfig command is responsible for creating the libFOO.so link to the latest version of libFOO.MAJOR.MINOR.so

A more extensive discussion can be found here: Dissecting shared libraries

share|improve this answer
Hi Miguel, thanks for that, shame I can't accept two answers because that complements the above nicely. +1 from me, excellent link too, thanks again! – Antony Vennard Aug 15 '10 at 14:22
2  
That is nearly correct, but it is actually libFOO.so.MAJOR.MINOR (so not at the end) – JonnyJD Mar 4 at 17:34

libNAME.so is the filename used by the compiler/linker when first looking for a library specified by -lNAME. Inside a shared library file is a field called the SONAME. This field is set when the library itself is first linked into a shared object (so) by the build process. This SONAME is actually what a linker stores in an executable depending on that shared object is linked with it. Normally the SONAME is in the form of libNAME.so.MAJOR and is changed anytime the library becomes incompatible with existing executables linked to it and both major versions of the library can be kept installed as needed (though only one will be pointed to for development as libNAME.so) Also, to support easily upgrading between minor versions of a library, libNAME.so.MAJOR is normally a link to a file like libNAME.so.MAJOR.MINOR. A new minor version can be installed and once completed, the link to the old minor version is bumped to point to the new minor version immediately upgrading all new executions to use the upgraded library. Also, see my answer to Linux, GNU GCC, ld, version scripts and the ELF binary format -- How does it work??

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.