next up previous contents
Next: Creating Static Libraries Up: Libraries Previous: What are Libraries ?   Contents

Types of Library

There are two types of library: Static and Dynamic/Shareable. A program can be built from either or a mixture of both.

Static Libraries
Static libraries really are nothing more than a collection of object files. As the linker processes a library it simply takes a copy of the object files it needs as if you had just compiled them. At the end of linking the executable (i.e. the program you will finally run) contains all your code and all the required library code so can be quite large. Static library files end with a .a suffix e.g. libZoo.a

Dynamic/Shareable Libraries
The other type of library is called a Dynamic or Shareable library. Both names give clues to additional features it has to offer. The input to this type of a library is the same as for static libraries i.e. object files, it is the way the linker handles them that is different. In this case it records the information necessary to load it into memory but does not add the code to the executable. So at the end of linking the executable is typically very small. During execution, if the code is called then it is loaded dynamically into shared memory that can be used by other programs. Dynamic library files end with a .so suffix e.g. libZoo.so

In general Dynamic/Shareable are better than Static ones because:-

  1. The executable is much smaller and so loads faster when you start the program.

  2. Object code is only loaded if its required as determined by the program flow. In a large complex program it often happens than only a small part is actually run for any particular job, but if statically loaded, the entire program always gets loaded into memory.

  3. The memory is more efficiently used; if several programs of the same type are running they can share a single copy of the object code.

  4. Addition dynamic libraries, not defined during linking, can be added during execution allowing existing executables to be extended after they were built. This is a very powerful feature. For example one of the standard analysis tools in HEP is the framework ROOT. It is possible to write your own code, compile and store in a dynamic library and then add it during a ROOT job to extend ROOT for your particular application.

There is just one drawback to dynamic libraries.

At the start of execution and while running, the system has to know where to find the dynamic libraries. If they have moved, or the program copied to another computer, then the program may break. If the libraries have changed then the behavior of the executable will change. On the other hand, if statically linked the executable is complete and stand-alone.


next up previous contents
Next: Creating Static Libraries Up: Libraries Previous: What are Libraries ?   Contents
P.D. Gronbech (IT Staff) 2015-10-02