Search code examples
cshared-libraries

how shared libraries reduces the space used by process ( data, code, etc)?


i have a doubt about how the fallowing same programing giving different sizes , without shared libraries , with shared libraries.?

      example :

      case 1 ( with out using libraries )          

      $ls -l a.out // in this program i didnt use shared libraries .                
      -rwxrwxr-x 1 friend 15555 aug 3 a.out

      $ size a.out 

      text    data bss  dec 
       4000   4555 0    8555 

       case 2 : ( using libraries )           

        $ls -l a.out // in this program used shared libraries .                
      -rwxrwxr-x 1 friend 8000 aug 3 a.out

      $ size a.out 

      text    data bss  dec 
      2000    2888 0    4888

note : all values i have take are approximate values to show its giving lees sizes in second case. so values may vary but indeed it will be less than the case1.


Solution

  • I believe that in the first case the linker is statically linking the libraries into the executable, and thus making it larger. An advantage of this, however, is that users of your program will not need to have to make sure that they have certain libraries that your program uses, since the libraries are packaged in your compiled binary.

    In your second case, it is using shared libraries, and so the libraries don't have to be packaged in the executable. Your program tries to find the libraries when it starts.