A dynamic library (shared library) can be linked with an executable at runtime.
The code of a shared library is loaded into physical memory once and can be reused by multiple processes
Extension used for dynamic libraries is .so
Creation of dynamic libraries is done using the –shared switch with gcc
Important options of the gcc command:
shared : produce a shared object which can then be linked with other objects to form an executable
fpic: generate position-independent code (PIC) suitable for use in a shared library
a.out: libsample.so program.o
gcc –L. program.o –lsample
program.o: program.c
gcc –c program.c
libsample.so: a.o b.o
gcc –shared –o libsample.so a.o b.o
a.o: a.c
gcc –fpic –c a.c
b.o: b.c
gcc –fpic –c b.c