| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | <sect1 id="ch05-whystatic"><title>Why we use static linking</title><?dbhtml filename="whystatic.html" dir="chapter05"?><para>Most programs have to perform, beside their specific task, many rathercommon and trivial operations, such as allocating memory, searchingdirectories, opening and closing files, reading and writing them, stringhandling, pattern matching, arithmetic, and so on.  Instead of obliging eachprogram to reinvent the wheel, the GNU system provides all these basicfunctions ready-made in libraries. The major library on any Linux system is<filename>glibc</filename>. To get an idea of what it contains, have a look at<filename>glibc/index.html</filename> somewhere on your host system.</para><para>There are two ways of linking the functions from a library to a programthat uses them: statically or dynamically. When a program is linkedstatically, the code of the used functions is included in the executable,resulting in a rather bulky program. When a program is dynamically linked,what is included is a reference to the linker, the name of the library, andthe name of the function, resulting in a much smaller executable. Thisexecutable has the disadvantage of being somewhat slower than a staticallylinked one, as the linking at run time takes a few moments.</para><para>Aside from this small drawback, dynamic linking has two major advantagesover static linking. First, you need only one copy of the executable librarycode on your hard disk, instead of having many copies of the same code includedinto a whole bunch of programs -- thus saving disk space. Second, when severalprograms use the same library function at the same time, only one copy of thefunction's code is required in core -- thus saving memory space.</para><para>Nowadays saving a few megabytes of space may not seem like much, butmany moons ago, when disks were measured in megabytes and core in kilobytes,such savings were essential. It meant being able to keep several programs incore at the same time and to contain an entire Unix system on just a few diskvolumes.</para><para>A third but minor advantage of dynamic linking is that when a libraryfunction gets a bug fixed, or is otherwise improved, you only need to recompilethis one library, instead of having to recompile all the programs that make useof the improved function.</para> <para>In summary we can say that dynamic linking trades run time againstmemory space, disk space, and recompile time.</para><para>But if dynamic linking saves so much space, why then are we linkingall programs in this chapter statically? The reason is that we won't becompiling a temporary <filename>glibc</filename> here. And we avoid doing thissimply to save some time -- around 14 SBUs. Another reason is that theGlibc version on the LFS system might not be compatible with the Glibc onthe host system. Applications compiled against your host system's Glibcversion may not run properly (or at all) on the LFS system.</para><para>This means that the tools compiled in this chapter will have to beself-contained, because when later on we chroot to the LFS partition theGNU library won't be available. That is why we use the<userinput>-static</userinput>, <userinput>--enable-static-link</userinput>,and <userinput>--disable-shared</userinput> flags throughout this chapter, toensure that all executables are statically linked. When we come to the nextchapter, almost the first thing we do is build <filename>glibc</filename>, themain set of system libraries. Once this is done, we can link all other programsdynamically (including the ones installed statically in this chapter) andtake advantage of the space saving opportunities.</para></sect1>
 |