Lesson 10

Dates: 4/10/2019
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
    

    Compile code.c with the Math shared library (default compilation):
    gcc code.c -o code_shared.x -lm
    
    Get the list of shared libraries required by code_stat.x, and code_shared.x:
    ldd code_stat.x
    ldd code_shared.x
    
    Compare the sizes of the executable files:
    ls -lh  code_*.x
    



  • Take me to the Course Website