Tutorial on Static and Dynamic libraries and their Linkage in C with Example

Libraries are the pre-compiled binary that can use with any program. Generally, libraries are shipped or distribute to others and they can develop their application with help of these libraries.
mostly any proprietory code shipped in form of libraries to others.

Static libraries

  • Static libraries contain multiple object files.
  • .a is the static libraries file extension.
  • Program size increased because of the static linkage of the static libraries.

To create a static and dynamic library, you need library source code, header file, and main program like below.

Create Library Source code and header file

Create a simple library source code test.c file

/* library function */
#include<stdio.h>
void func(void){

    printf("called func from library\n");

}

Need to create the header file for the library test.h

/* header file function declaration */
void func(void);

Create the main function

Create a main program demo.c file that uses the library function to execute your main program. this way you can be shipped the library to others and they can utilize the static library to create your own application.

/* Static library main program */

#include<stdio.h>
#include"test.h"
void main(){

    printf("calling the library function\n");
    func();
}

Steps to Create the Static library

On Linux based system, you need to follow the below step to create the static library.

  1. Compile the library file

    Compile the library file with gcc to generate the object file (.o) with the below command

    # test.c is library source file
    $ gcc -c test.c 
    
  2. Create the static library (.a)

    Using ar tool create static library image

    # Run below command 
    $ ar rcs libtest.a test.o
    
    static library command
  3. Compilation and Linkage of static library

    Compile the main program and link it with the static library to run the program.

    # main program compilation with below command 
    $ gcc demo.c -o demo 
    # This command gives the below error and the program will not compile.tmp/ccIIpV9u.o: In function `main':
    demo.c:(.text+0xf): undefined reference to `func'
    collect2: error: ld returned 1 exit status

    It gives the above error because the compiler does not know what is func in the main function so we need to point to the static library which has a definition of the func.

    # Run below command to link with static library
    $ gcc demo.c -L. -ltest -o demo
    
    -L. -> look for the library in the current directory
    -l   -> link with the library file
    -o  -> output of the program binary
    
    # Execute the program binary 
    $ ./demo
    

    static linkage with c program

Dynamic libraries

  • Dynamic libraries are the shared object with .so file extensions.
  • Dynamic library linked to the program during the program initialization or run time.
  • Due to dynamic linkage, the program binary size is less as compared to static linkage.

Steps to Create the Dynamic library

Follow the below steps to create the dynamic library

  1. Compile the library source file

    This creates an object file of the library source file

    # Run below command 
    $ gcc -c -fpic test.c
    
  2. Creating shared library(.so ) file
    # Create shared library with below command
    $ gcc -shared -o libtest.so test.o
    Dynamic library command
  3. Compilation and link with the dynamic library

    We have generated the dynamic library (libtest.so) and need to link this library with the main program to execute the binary.

    # Run below command 
    $ gcc demo.c -ltest -o demo
    # You will get this error
    /usr/bin/ld: cannot find -ltest
    collect2: error: ld returned 1 exit status
    

    You will get the above error because the compiler does not know the location of the library so provide the path of the library

    # Set path of the library
    $ gcc -L/home/tutorialadda/test/ -o demo demo.c -ltest
    Dynamic linkage error
  4. Execute the Program binary

    We have already created the dynamic library (.so ) file and program binary is created

    .
    # Run the below command
    $ ./demo
    #You will get the below error
    ./demo: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
    

    Above error, because the library is not present at the standard path and the loader can find it. you need to export LD_LIBRARY_PATH for this.

    #export path of existing LD_LIBRARY_PATH variable.
    $ export LD_LIBRARY_PATH=/home/tutorialadda/test/:$LD_LIBRARY_PATH
    # Again run the program 
    $ ./demo
    
    Dynamic linkage

Add comment


Security code
Refresh