Wednesday, June 22, 2011

Static v/s Dynamic Library

What is a static library?

First of all, a library is a set of functions complied into an object code that is used by applications. When we compile a program that uses a static library, the entire object code gets included in the executable code thus increasing the size of executable. Lets say, the library is altered. Now in this case, you need to recompile the library and recompile your application. The other disadvantage is that if there are several applications running that use the static library, then it leads to wastage of space as the library is included in all the executables. Static libraries are also called as archives(.a extension). Try exploring the makefile, it will have loads of .a libraries in the dependency list.

Dynamic Library:
It consists of routines or functions that are loaded at runtime if a reference exists. In this case, instead of including the entire object code, we include a reference to the library and define the rules on how to find this reference(basically tell the linker where to locate the library for eg, /usr/lib). You can keep it anywhere but just mention the path in the linker search path. They have the advantage that if your library gets changes, the application using the library need not be compiled, it just keeps running. If more than one application using this dynamic library is running, then only a single copy is loaded into the memory which is used by all the applications. These have ".so" extension.