next up previous
Next: The Shared C++ Up: ELF Support in Previous: ELF Support in

The Shared C Library

Building a shared library has never been easier under ELF. But we need ELF support in the compiler, assembler and link editor. First we need to generate position-independent code. Under gcc, that is done by adding -fPIC to the compiler options:

# gcc -fPIC -O -c libbar.c

We now have compiled libbar.o suitable for building the shared library. Next we can generate the shared library by

# gcc -shared -o libbar.so libbar.o

We now have built the shared library libbar.so which can be used by the link editor and dynamic linker. One can add as many relocatable object files to a shared library as long as they are compiled with -fPIC. To link baz.o with the shared library you can just do

# gcc -O -c baz.c
# gcc -o baz  baz.o -L. -lbar

After installing libbar.so in the right place where the dynamic linker can find it, running baz will make libbar.so map into the address space of the process image of baz. One copy of libbar.so in memory will be shared by all executables which are linked with it or which load it dynamically at run time.



J.H.M.Dassen
Tue Aug 1 14:18:10 MDT 1995