TOC   Previous   Next

Creating Shared Libraries Using Standard Naming Conventions

We can create a shared library using standard naming conventions as follows:

  1. Create the shared library with real name libdemo.so.1.0.1 and soname libdemo.so.1.
    $ gcc -fPIC -g -c -Wall mod1.c mod2.c mod3.c
    $ gcc -shared -Wl,-soname,libdemo.so.1 -o libdemo.so.1.0.1 \
              mod1.o mod2.o mod3.o
    
  2. Create symbolic links for the soname and linker name:
    $ ln -s libdemo.so.1.0.1 libdemo.so.1
    $ ln -s libdemo.so.1 libdemo.so
    $ ls -l libdemo.so* | cut -c 1-11,55-        # Verify the setup
    lrwxrwxrwx  libdemo.so -> libdemo.so.1
    lrwxrwxrwx  libdemo.so.1 -> libdemo.so.1.0.1
    -rwxr-xr-x  libdemo.so.1.0.1
    
  3. Build executable using the linker name:
    $ gcc -g -Wall -o ./prog prog.c -L. -ldemo
    
  4. Run the program as usual:
    $ LD_LIBRARY_PATH=. ./prog
    Called mod1-x1
    Called mod2-x2
    

(C) 2006, Michael Kerrisk