Sunday, November 11, 2012

Static and Dynamic Libraries in C in Linux

Hi All...!!!

I am writing this post as a explanation for the doubts raised by few of the readers of my blog. I will be discussing about Static and Dynamic libraries in C in Linux with a sample code.

Libraries are also known as "shared components" or "archive libraries" which groups together multiple compiled object code files into a single file known as a library. 

Typically C functions which can be shared by more than one application are broken out of the application's source code, compiled and bundled into a library. The C standard libraries are examples of shared components which can be linked with your code.

The benefit is that each and every object file need not be stated when linking because the developer can reference the individual library. This simplifies the multiple use and sharing of software components between applications. It also allows application vendors a way to simply release an API to interface with an application. 

Components which are large can be created for dynamic use, thus the library remain separate from the executable reducing it's size and thus disk space used. The library components are then called by various applications for use when needed.

There are two Linux C library types which can be created:

Static libraries: Library of object code which is linked with, and becomes part of the application.


Dynamically linked shared object libraries: There is only one form of this library but it can be used in two ways.

  • Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
  • Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.

Library naming conventions:
  • Libraries are typically names with the prefix "lib", this is true for all C standard libraies
  • Static libraries are with extension (.a)
  • Dynamic or Shared libraries are with extension (.so)
Static libraries are loaded when the program is compiled and Dynamically-linked libraries are loaded at run time. Dynamic libraries save the RAM space as against the Static library because linking to static libraries includes the actual code for the library functions with the executable. DLL code is kept at one location and is shared among all the processes/application that uses the DLL.


No comments:

Post a Comment