source: chapter05/whystatic.xml@ 05ed965

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.0 6.1 6.1.1 6.3 6.4 6.5 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk v5_0 v5_1 v5_1_1 xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since 05ed965 was 255504f, checked in by James Robertson <jwrober@…>, 21 years ago

Chapter 5 - Updated the Why Static page to more accurately represent the difference between statically and dynamically linked binaries. Thanks to Ian Molton for point this out. Fixes Bug 602. Updated bookinfo with this year in copyright.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@2868 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

  • Property mode set to 100644
File size: 3.5 KB
Line 
1<sect1 id="ch05-whystatic">
2<title>Why we use static linking</title>
3<?dbhtml filename="whystatic.html" dir="chapter05"?>
4
5<para>Most programs have to perform, beside their specific task, many rather
6common and trivial operations, such as allocating memory, searching
7directories, opening and closing files, reading and writing them, string
8handling, pattern matching, arithmetic, and so on. Instead of obliging each
9program to reinvent the wheel, the GNU system provides all these basic
10functions ready-made in libraries. The major library on any Linux system is
11<filename>glibc</filename>. To get an idea of what it contains, have a look at
12<filename>glibc/index.html</filename> somewhere on your host system.</para>
13
14<para>There are two ways of linking the functions from a library to a program
15that uses them: statically or dynamically. When a program is linked
16statically, the code of the used functions is included in the executable,
17resulting in a rather bulky program. When a program is dynamically linked,
18what is included is a reference to the linker, the name of the library, and
19the name of the function, resulting in a much smaller executable. Under
20certain circumstances, this executable can have the disadvantage of being
21somewhat slower than a statically linked one, as the linking at run time takes
22a few moments. It should be noted, however, that under normal circumstances on
23today's hardware, a dynamically linked executable will be faster than a
24statically linked one as the library function being called by the dynamically
25linked executable has a good chance of already being loaded in your system's
26RAM.</para>
27
28<para>Aside from this small drawback, dynamic linking has two major advantages
29over static linking. First, you need only one copy of the executable library
30code on your hard disk, instead of having many copies of the same code included
31into a whole bunch of programs -- thus saving disk space. Second, when several
32programs use the same library function at the same time, only one copy of the
33function's code is required in core -- thus saving memory space.</para>
34
35<para>Nowadays saving a few megabytes of space may not seem like much, but
36many moons ago, when disks were measured in megabytes and core in kilobytes,
37such savings were essential. It meant being able to keep several programs in
38core at the same time and to contain an entire Unix system on just a few disk
39volumes.</para>
40
41<para>A third but minor advantage of dynamic linking is that when a library
42function gets a bug fixed, or is otherwise improved, you only need to recompile
43this one library, instead of having to recompile all the programs that make use
44of the improved function.</para>
45
46<para>In summary we can say that dynamic linking trades run time against
47memory space, disk space, and recompile time.</para>
48
49<para>But if dynamic linking saves so much space, why then are we linking
50the first two packages in this chapter statically? The reason is to make them
51independent from the libraries on your host system. The advantage is that, if
52you are pressed for time, you could skip the second passes over GCC and
53Binutils, and just use the static versions to compile the rest of this chapter
54and the first few packages in the next. In the next chapter we will be
55chrooted to the LFS partition and once inside the chroot environment, the host
56system's Glibc won't be available, thus the programs from GCC and Binutils
57will need to be self-contained, i.e. statically linked. However, we strongly
58advise <emphasis>against</emphasis> skipping the second passes.</para>
59
60</sect1>
61
Note: See TracBrowser for help on using the repository browser.