whystatic.xml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <sect1 id="ch05-whystatic">
  2. <title>Why do we use static linking?</title>
  3. <?dbhtml filename="whystatic.html" dir="chapter05"?>
  4. <para>Thanks to Plasmatic for posting the text on which this is mainly
  5. based to one of the LFS mailing lists.</para>
  6. <para>When making (compiling) a program, rather than having to rewrite all the
  7. functions for dealing with the kernel, hardware, files, etc. everytime you
  8. write a new program, all these basic functions are instead kept in libraries.
  9. glibc, which you install later, is one of these major libraries, which contain
  10. code for all the basic functions programs use, like opening files, printing
  11. information on the screen, and getting feedback from the user. When the
  12. program is compiled, these libraries of code are linked together with the new
  13. program, so that it can use any of the functions that the library
  14. has.</para>
  15. <para>However, these libraries can be very large (for example, libc.a
  16. from can often be around 2.5MB), so you may not want a seperate copy of
  17. each library attached to the
  18. program. Just imagine if you had a simple command like ls with an extra 2.5MB
  19. attached to it! Instead of making the library an actual part of the
  20. program, or Statically Linked, the library is kept a seperate file,
  21. which is loaded only when the program needs it. This is what we call Dynamically
  22. Linked, as the library is loaded and unloaded dynamically, as the program needs
  23. it.</para>
  24. <para>So now we have a 1kb file and a 2.5MB file, but we still haven't saved any
  25. space (except maybe RAM until the library is needed). The REAL advantage to
  26. dynamically linked libraries is that we only need one copy of the library.
  27. If <filename>ls</filename> and <filename>rm</filename>both use the same
  28. library, then we don't need two copies of the
  29. library, as they can both get the code from the same file.
  30. Even when in memory, both programs share the same code, rather than loading
  31. duplicates into memory. So not only are we saving hard disk space, but also
  32. precious RAM.</para>
  33. <para>If dynamic linking saves so much room, then why are we making everything
  34. statically linked? Well, that's because when you chroot into your brand new
  35. (but very incomplete) LFS environment, these dynamic libraries won't be
  36. available because they are somewhere else in your old directory tree
  37. (<filename>/usr/lib</filename> for example) which won't be accessible
  38. from within your LFS root (<filename>$LFS</filename>).</para>
  39. <para>So in order for your new programs to run inside the chroot environment you
  40. need to make sure that the libraries are statically linked when you build
  41. them, hence the <userinput>--enable-static-link</userinput>,
  42. <userinput>--disable-shared</userinput>, and
  43. <userinput>-static</userinput> flags used
  44. through Chapter 5. Once in Chapter 6, the first thing we do is build the
  45. main set of system libraries, glibc. Once this is made we start rebuilding
  46. all the programs we just did in Chapter 5, but this time dynamically linked,
  47. so that we can take advantage of the space saving opportunites.</para>
  48. <para>And there you have it, that's why you need to use those weird
  49. <userinput>-static</userinput> flags. If you try building everything
  50. without them, you'll see very quickly what
  51. happens when you chroot into your newly crippled LFS system.</para>
  52. <para>If you want to know more about Dynamically Linked Libraries, consult a
  53. book or website on programming, especially a Linux-related site.</para>
  54. </sect1>