source: chapter05/whystatic.xml@ fd132b0

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 v4_0 v4_1 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 fd132b0 was 921ef9f, checked in by Marc Heerdink <gimli@…>, 22 years ago

Fixed some minor typos

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

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