Shared Libraries in 3 steps

What are Shared Libraries?
  • Libraries with the .so extension which can be shared among multiple programs thus greatly reducing the memory footprint.
  • Libraries can be updated without affecting the user code.
  • Multiple versions of libraries can stay together and the user code can link to required version.2.
  •     
How to create a shared library?
  • First create a test.c file with the function implementation.
  • Create an object file test.o  
          gcc -c test.c
  • Create the shared library using the following command
          gcc -shared -o libtest.so test.o -Lpwd
   
How to use a shared library?
  • First write a c code main.c that uses the function used in test.c.
  • Put the location of library in LD_LIBRARY_PATH
  • Now we need to compile the main.c with the linking info about the shared library.
         gcc -o test main.c -ltest     //Note that here ltest corresponds to libtest.so

    
          

 

No comments:

Post a Comment