Spread the love

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, libxyz.so.1.2 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 libctest.so.1“. 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 “libxyz.so.1.2

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 :
    #include "SharedLib.h"
    unsigned int multiply(unsigned int a, unsigned int b)
    {
        printf("\n Inside Multiply function\n");
        return (a*b);
    }

    2. SharedLib.h looks like :

    #include<stdio.h>
    extern unsigned int multiply(unsigned int a, unsigned int b);

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

    gcc -c -Wall -Werror -fPIC SharedLib.c
    gcc -Shared -o libshared.so shared.o

    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 ‘libshared.so’.

  2. Here is the code of the program that uses the shared library function ‘multiply()’ :
    #include<stdio.h>
    #include"SharedLib.h"
    void main()
    {
        unsigned int a ;
        unsigned int b ;
        unsigned int result = 0;
        printf("Enter first number :\n");
        scanf("%d",&a);
        printf("Enter second number :\n");
        scanf("%d",&b);
        result = multiply(a,b);
    
        printf("\n The result after multiplication is  [%d]\n",result);
        
    }

    Now Make a executable binary of this –

    gcc -Wall -o main main.c -L./ -lshared

    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 :
    export LD_LIBRARY_PATH=(give here the path of library):$LD_LIBRARY_PATH
    

    The above command exports the path to the environment variable ‘LD_LIBRARY_PATH’.

  4. Now run the executable ‘main’ :
    # ./main
    Enter first number :
    2
    Enter second number :
    3
    Inside Multiply function
    The result after multiplication is  [6]