whystatic.xml 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 form 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. all programs in this chapter statically? The reason is that we won't be
  39. compiling a temporary <filename>glibc</filename> here. And we avoid doing this
  40. simply to save some time -- around 14 SBUs. Another reason is that the
  41. Glibc version on the LFS system might not be compatible with the Glibc on
  42. the host system. Applications compiled against your host system's Glibc
  43. version may not run properly (or at all) on the LFS system.</para>
  44. <para>This means that the tools compiled in this chapter will have to be
  45. self-contained, because when later on we chroot to the LFS partition the
  46. GNU library won't be available. That is why we use the
  47. <userinput>-static</userinput>, <userinput>--enable-static-link</userinput>,
  48. and <userinput>--disable-shared</userinput> flags throughout this chapter, to
  49. ensure that all executables are statically linked. When we come to the next
  50. chapter, almost the first thing we do is build <filename>glibc</filename>, the
  51. main set of system libraries. Once this is done, we can link all other programs
  52. dynamically (including the ones installed statically in this chapter) and
  53. take advantage of the space saving opportunities.</para>
  54. </sect1>