Lesson 11

Dates: 4/5/2017
Application compilation and installation on Linux
Linux System Administration


Shared libraries


  • Shared libraries, unlike static ones, are not compiled into the application binaris. They are loaded into the memory and linked with the application at a run time. Other running codes can use the loaded shared libraries.



  • Building a shared library
    gcc -c -fPIC lib1.c
    gcc -c -fPIC lib2.c
    gcc -c -fPIC lib3.c
    gcc -shared lib1.o lib2.o lib3.o -o libextra.so
    
  • To compile the application with the library we would follow the step below:
    gcc -o code.x code.c -L. -lextra 
    
  • To see what the shared libraries the code needs, use ldd command
    ldd code.x
    
  • If the library is not found, include it into LD_LIBRARY_PATH environment variable, for example:
    export LD_LIBRARY_PATH=. 
    
  • Alternatively the shared library path can be included into /etc/ld.so.conf. After /etc/ld.so.conf has been updated, we need to run command
    /sbin/ldconfig -p
    

  • Exercise
    Get the list of shared libraries required by command, ls:
    ldd /bin/ls
    



  • Take me to the Course Website