whystatic.xml 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. Under
  18. certain circumstances, this executable can have the disadvantage of being
  19. somewhat slower than a statically linked one, as the linking at run time takes
  20. a few moments. It should be noted, however, that under normal circumstances on
  21. today's hardware, a dynamically linked executable will be faster than a
  22. statically linked one as the library function being called by the dynamically
  23. linked executable has a good chance of already being loaded in your system's
  24. RAM.</para>
  25. <para>Aside from this small drawback, dynamic linking has two major advantages
  26. over static linking. First, you need only one copy of the executable library
  27. code on your hard disk, instead of having many copies of the same code included
  28. into a whole bunch of programs -- thus saving disk space. Second, when several
  29. programs use the same library function at the same time, only one copy of the
  30. function's code is required in core -- thus saving memory space.</para>
  31. <para>Nowadays saving a few megabytes of space may not seem like much, but
  32. many moons ago, when disks were measured in megabytes and core in kilobytes,
  33. such savings were essential. It meant being able to keep several programs in
  34. core at the same time and to contain an entire Unix system on just a few disk
  35. volumes.</para>
  36. <para>A third but minor advantage of dynamic linking is that when a library
  37. function gets a bug fixed, or is otherwise improved, you only need to recompile
  38. this one library, instead of having to recompile all the programs that make use
  39. of the improved function.</para>
  40. <para>In summary we can say that dynamic linking trades run time against
  41. memory space, disk space, and recompile time.</para>
  42. <para>But if dynamic linking saves so much space, why then are we linking
  43. the first two packages in this chapter statically? The reason is to make them
  44. independent from the libraries on your host system. The advantage is that, if
  45. you are pressed for time, you could skip the second passes over GCC and
  46. Binutils, and just use the static versions to compile the rest of this chapter
  47. and the first few packages in the next. In the next chapter we will be
  48. chrooted to the LFS partition and once inside the chroot environment, the host
  49. system's Glibc won't be available, thus the programs from GCC and Binutils
  50. will need to be self-contained, i.e. statically linked. However, we strongly
  51. advise <emphasis>against</emphasis> skipping the second passes.</para>
  52. </sect1>