whystatic.xml 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <sect1 id="ch05-whystatic">
  2. <title>Why we use static linking</title>
  3. <?dbhtml filename="whystatic.html" dir="chapter05"?>
  4. <para>Most programs have to perform, beside their specific task, many rather
  5. common and trivial operations, such as allocating memory, searching
  6. directories, opening and closing files, reading and writing them, string
  7. handling, pattern matching, arithmetic, and so on. Instead of obliging each
  8. program to reinvent the wheel, the GNU system provides all these basic
  9. functions ready-made in libraries. The major library on any Linux system is
  10. <filename>glibc</filename>. To get an idea of what it contains, have a look at
  11. <filename>glibc/index.html</filename> somewhere on your host system.</para>
  12. <para>There are two ways of linking the functions from a library to a program
  13. that uses them: statically or dynamically. When a program is linked
  14. statically, the code of the used functions is included in the executable,
  15. resulting in a rather bulky program. When a program is dynamically linked,
  16. what is included is a reference to the linker, the name of the library, and
  17. the name of the function, resulting in a much smaller executable. This
  18. executable has the disadvantage of being somewhat slower than a statically
  19. linked one, as the linking at run time takes a few moments.</para>
  20. <para>Aside from this small drawback, dynamic linking has two major advantages
  21. over static linking. First, you need only one copy of the executable library
  22. code on your hard disk, instead of having many copies of the same code included
  23. into a whole bunch of programs -- thus saving disk space. Second, when several
  24. programs use the same library function at the same time, only one copy of the
  25. function's code is required in core -- thus saving memory space.</para>
  26. <para>Nowadays saving a few megabytes of space may not seem like much, but
  27. many moons ago, when disks were measured in megabytes and core in kilobytes,
  28. such savings were essential. It meant being able to keep several programs in
  29. core at the same time and to contain an entire Unix system on just a few disk
  30. volumes.</para>
  31. <para>A third but minor advantage of dynamic linking is that when a library
  32. function gets a bug fixed, or is otherwise improved, you only need to recompile
  33. this one library, instead of having to recompile all the programs that make use
  34. of the improved function.</para>
  35. <para>In summary we can say that dynamic linking trades run time against
  36. memory space, disk space, and recompile time.</para>
  37. <para>But if dynamic linking saves so much space, why then are we linking
  38. the first two packages in this chapter statically? The reason is to make them
  39. independent from the libraries on your host system. And the point in that is
  40. that, if you are pressed for time, you could skip the second passes over GCC
  41. and Binutils, and just use the static versions to compile the rest of this
  42. chapter and the first few packages in the next. As in the next chapter we
  43. will be chrooted to the LFS partition and your host system's Glibc won't be
  44. available, the programs from GCC and Binutils will need to be self-contained,
  45. that is statically linked.</para>
  46. </sect1>