TOC   Previous   Next

Creating and using a shared library - pass 1

(This is a first try, to demonstrate principles- we'll look at the "proper" method later)

  1. Create the object modules using -fPIC:
    $ gcc -fPIC -g -c -Wall mod1.c mod2.c mod3.c
    
    -fPIC (Position Independent Code) causes compiler to generate code that can be loaded anywhere in virtual memory at run-time.
  2. Create the shared library:
    $ gcc -shared -o libfoo.so mod1.o mod2.o mod3.o
    
    Usual naming convention: libname.so[.version]
  3. Link program against library :

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

    or:

    $ gcc -g -Wall -o prog prog.c -L. -lfoo
    
    This step embeds name of shared library inside executable file.

(C) 2006, Michael Kerrisk