| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | <sect1 id="ch05-whystatic"><title>Why do we use static linking?</title><?dbhtml filename="whystatic.html" dir="chapter05"?><para>(Thanks to Plasmatic for posting the text on which this is mainlybased to one of the LFS mailing lists.)</para><para>When making (compiling) a program, rather than having to rewrite all thefunctions for dealing with the kernel, hardware, files, etc. every time youwrite a new program, all these basic functions are instead kept in libraries.glibc, which you install later, is one of these major libraries, whichcontains code for all the basic functions programs use, like opening files,printing information on the screen, and getting feedback from the user. Whenthe program is compiled, these libraries of code are linked together with thenew program, so that it can use any of the functions that the libraryhas.</para><para>However, these libraries can be very large (for example, libc.acan often be around 2.5 MB), so you may not want a separate copy of eachlibrary attached to the program. Just imagine if you had a simple commandlike ls with an extra 2.5 MB attached to it! Instead of making the libraryan actual part of the program, or statically linked, the library is storedas a separate file, which is loaded only when the program needs it. Thisis what we call dynamically linked, as the library is loaded and unloadeddynamically, as the program needs it.</para><para>So now we have a 1 KB file and a 2.5 MB file, but we still haven'tsaved any space (except maybe RAM until the library is needed). The<emphasis>real</emphasis> advantage of dynamically linked libraries isthat we only need one copy of the library. If <filename>ls</filename> and<filename>rm</filename> both use the same library, then we don't need twocopies of the library, as they can both get the code from the same file.Even when in memory, the two programs share the same code, rather than loadingduplicates into memory. So not only are we saving hard disk space, but alsoprecious RAM.</para><para>If dynamic linking saves so much room, then why are we making everythingstatically linked? Well, that's because when you chroot into your brand new(but very incomplete) LFS environment, these dynamic libraries won't beavailable because they are somewhere else in your old directory tree(<filename>/usr/lib</filename> for example) which won't be accessible from within your LFS root (<filename>$LFS</filename>).</para><para>So in order for your new programs to run inside the chroot environmentyou need to make sure that the libraries are statically linked when you buildthem, hence the <userinput>--enable-static-link</userinput>, <userinput>--disable-shared</userinput>, and<userinput>-static</userinput> flags usedthrough Chapter 5. Once in Chapter 6, the first thing we do is build themain set of system libraries, glibc. Once this is made we start rebuilding all the programs we just did in Chapter 5, but this time dynamically linked, so that we can take advantage of the space saving opportunities.</para><para>And there you have it, that's why you need to use those weird<userinput>-static</userinput> flags. If you try building everything without them, you'll see very quickly whathappens when you chroot into your newly crippled LFS system.</para><para>If you want to know more about Dynamically Linked Libraries, consulta book or website on programming, especially a Linux-related site.</para></sect1>
 |