Embedded Systems

How to make Shared Library in LINUX

Linux Library Types:

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

  1. Static libraries (.a): Library of object code which is linked with, and becomes part of the application. To know more about Static Libraries, Click Here
  2. Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
    1. 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.
    2. Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.

Shared Libraries

Shared libraries are libraries that are loaded by programs when they start. When a shared library is installed properly, all programs that start afterwards automatically use the new shared library. It’s actually much more flexible and sophisticated than this, because the approach used by Linux permits you to:

  • update libraries and still support programs that want to use older, non-backward-compatible versions of those libraries;
  • override specific libraries or even specific functions in a library when executing a particular program.
  • do all this while programs are running using existing libraries.

Every shared library has a special name called the “soname”. The soname has the prefix “lib”, the name of the library, the phrase “.so”, followed by a period and a version number that is incremented whenever the interface changes. For example, etc.

A version number is changed for a shared library when the changes done in the code make the shared library incompatible with the previous version.

Compiler options used to Create Shared Libraries:

  • -Wall: include warnings. See man page for warnings specified.
  • -fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries. Also see “-fpic”.
  • -shared: Produce a shared object which can then be linked with other objects to form an executable.
  • -Wl,options: Pass options to linker.
    In this example the options to be passed on to the linker are: “-soname“. The name passed with the “-o” option is passed togcc.
  • Option -o: Output of operation. In this case the name of the shared object to be output will be “

Location of Library in File System :

There are mainly three standard locations in the file system where a library can be placed.

  • /lib
  • /usr/lib
  • /usr/local/libl

How to Create a Shared Library :

  1. Let us take a piece of code (shared.c) that we want to put in a shared library :

    2. SharedLib.h looks like :

    3.Run the following two commands to create a shared library :

    The first command compiles the code shared.c into position independent code which is required for a shared library.
    The second command actually creates a shared library with name ‘’.
  2. Here is the code of the program that uses the shared library function ‘multiply()’ :

    Now Make a executable binary of this –

    Here after -L “./” means current directory is the location of library. OR you can give the address where you kept your library and “-l” small ‘l’ indicates the name of shared library.
  3. Now, export the path where the newly created shared library is kept by using the following command :

    The above command exports the path to the environment variable ‘LD_LIBRARY_PATH’.
  4. Now run the executable ‘main’ :