The shared library soname (cont.)

Here's how to use a soname:

  1. Specify soname when creating shared library:

  2. $ gcc -fPIC -c -Wall -g mod1.c mod2.c mod3.c 
    $ gcc -shared -Wl,-soname,libbar.so -o libfoo.so \
    mod1.o mod2.o mod3.o


  3. -Wl,-soname,libbar.so instructs linker to mark the shared library libfoo.so with the sonamelibbar.so.
  4. Create executable:

  5. $ gcc -g -Wall -o prog prog.c libfoo.so


  6. Linker detects that libfoo.so contains the soname
  7. libbar.soand embeds the latter name inside the executable.
  8. Run the program:

  9. $ LD_LIBRARY_PATH=. ./prog
    ./prog: error in loading shared libraries:
    libbar.so: cannot open shared object file:
    No such file or directory


  10. Dynamic linker cannot find anything named libbar.so.
  11. Create a symbolic link from the soname to the real name of the library:

  12. $ ln -s libfoo.so libbar.so
    $ LD_LIBRARY_PATH=. ./prog
    Called mod1-x1
    Called mod2-x2


At run-time thislink can point to a version of the library which is different from the version against which linking was performed.