Static Libraries

A static library file is a file that consists of an archive of object files (.o files) collected into one file by the ar program. Static libraries usually end with .a ( .lib in Windows). At link time, static libraries are searched for each global function or variable symbol. If the symbol is found then the code for that symbol is copied into the binary. In addition, any other symbols that were in the original .o file for the symbol in question are also copied into the binary. In this way, if we need a symbol that is dependent on other functions in the .o file in which it is defined, at link time we get the dependent functions.  Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program.

Shared Libraries

Shared libraries allow multiple programs to share a library on disk, rather than copying code into a binary, resulting in smaller binaries. A shared library consists of an archive of object files (.o files) collected into one file by either the compiler or the linker. The constraint with shared libraries is that the binary be able to find the shared libraries at run time. Exactly how this is done is architecture dependent, but in general the runtime linker looks for special environment variable that contains path names for directories to be searched. Shared libraries are .so (or in Windows .dll, or in OS X .dylib) linked dynamically simply including the address of the library (whereas static linking is a waste of space). Dynamic linking links the libraries at the run-time. Thus, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them. A program using a shared library only makes reference to the code that it uses in the shared library.

Static Vs Shared Libraries

Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.

Static libraries increase the overall size of the binary, but it means that you don’t need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.