1 | <?xml version="1.0" encoding="UTF-8"?>
|
---|
2 | <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
---|
3 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
|
---|
4 | <!ENTITY % general-entities SYSTEM "../../general.ent">
|
---|
5 | %general-entities;
|
---|
6 | ]>
|
---|
7 |
|
---|
8 | <sect1 id="libraries" xreflabel="libraries">
|
---|
9 | <?dbhtml filename="libraries.html"?>
|
---|
10 |
|
---|
11 |
|
---|
12 | <title>Libraries: Static or shared?</title>
|
---|
13 |
|
---|
14 | <!-- section g : 'Others' in longindex.html -->
|
---|
15 | <indexterm zone="libraries">
|
---|
16 | <primary sortas="g-libraries">libraries: static or shared</primary>
|
---|
17 | </indexterm>
|
---|
18 |
|
---|
19 | <sect2 role="package">
|
---|
20 | <title>Libraries: Static or shared?</title>
|
---|
21 |
|
---|
22 | <para>
|
---|
23 | The original libraries were simply an archive of routines from which
|
---|
24 | the required routines were extracted and linked into the executable
|
---|
25 | program. These are described as static libraries, with names of the form
|
---|
26 | <filename>libfoo.a</filename> on UNIX-like operating systems.
|
---|
27 | On some old operating systems they are the only type available.
|
---|
28 | </para>
|
---|
29 |
|
---|
30 | <para>
|
---|
31 | On almost all Linux platforms there are also <quote>shared</quote>
|
---|
32 | (or equivalently <quote>dynamic</quote>)
|
---|
33 | libraries (with names of the form <filename>libfoo.so</filename>) –
|
---|
34 | one copy of the library is loaded into virtual memory, and shared by
|
---|
35 | all the programs which call any of its functions. This is space
|
---|
36 | efficient.
|
---|
37 | </para>
|
---|
38 |
|
---|
39 | <para>
|
---|
40 | In the past, essential programs such as a shell were often linked
|
---|
41 | statically so that some form of minimal recovery system would exist even
|
---|
42 | if shared libraries, such as <filename>libc.so</filename>, became
|
---|
43 | damaged (e.g. moved to <filename class="directory">lost+found</filename>
|
---|
44 | after <command>fsck</command> following an unclean shutdown). Nowadays,
|
---|
45 | most people use an alternative system install or a USB stick if they
|
---|
46 | have to recover. Journaling filesystems also reduce the likelihood of
|
---|
47 | this sort of problem.
|
---|
48 | </para>
|
---|
49 |
|
---|
50 | <!-- really?
|
---|
51 | <para>
|
---|
52 | Developers, at least while they are developing, often prefer to use
|
---|
53 | static versions of the libraries which their code links to.
|
---|
54 | </para>
|
---|
55 | -->
|
---|
56 | <para>
|
---|
57 | Within the book, there are various places where configure switches
|
---|
58 | such as <parameter>--disable-static</parameter> are employed, and
|
---|
59 | other places where the possibility of using system versions of
|
---|
60 | libraries instead of the versions included within another package is
|
---|
61 | discussed. The main reason for this is to simplify updates of libraries.
|
---|
62 | </para>
|
---|
63 |
|
---|
64 | <para>
|
---|
65 | If a package is linked to a dynamic library, updating to a newer
|
---|
66 | library version is automatic once the newer library is installed and the
|
---|
67 | program is (re)started (provided the library major version is unchanged,
|
---|
68 | e.g. going from <filename>libfoo.so.2.0</filename> to
|
---|
69 | <filename>libfoo.so.2.1</filename>. Going to
|
---|
70 | <filename>libfoo.so.3</filename> will require recompilation –
|
---|
71 | <command>ldd</command> can be used to find which programs use the old
|
---|
72 | version). If a program is linked to a static
|
---|
73 | library, the program always has to be recompiled. If you know which
|
---|
74 | programs are linked to a particular static library, this is merely an
|
---|
75 | annoyance. But usually you will <emphasis>not</emphasis> know which
|
---|
76 | programs to recompile.
|
---|
77 | </para>
|
---|
78 | <!-- obsolete with /usr merge
|
---|
79 | <para>Most libraries are shared, but if you do something unusual, such as
|
---|
80 | moving a shared library to <filename class="directory">/lib</filename>
|
---|
81 | accidentally breaking the <literal>.so</literal> symlink in
|
---|
82 | <filename class="directory">/usr/lib</filename> while keeping the static
|
---|
83 | library in <filename class="directory">/lib</filename>, the static library
|
---|
84 | will be silently linked into the programs which need it.</para>
|
---|
85 | -->
|
---|
86 | <para>
|
---|
87 | One way to identify when a static library is used, is to deal with
|
---|
88 | it at the end of the installation of every package. Write a script
|
---|
89 | to find all the static libraries in <filename
|
---|
90 | class="directory">/usr/lib</filename> or wherever you are installing
|
---|
91 | to, and either move them to another directory so that they are no
|
---|
92 | longer found by the linker, or rename them so that
|
---|
93 | <filename>libfoo.a</filename> becomes
|
---|
94 | e.g. <filename>libfoo.a.hidden</filename>. The static library can then
|
---|
95 | be temporarily restored if it is ever needed, and the package needing
|
---|
96 | it can be identified. This shouldn't be done blindly since many
|
---|
97 | libraries only exist in a static version. For example, some libraries
|
---|
98 | from the <application>glibc</application> and
|
---|
99 | <application>gcc</application> packages should always be
|
---|
100 | present on the system (<filename>libc_nonshared.a, libg.a,
|
---|
101 | libpthread_nonshared.a, libssp_nonshared.a, libsupc++.a</filename>
|
---|
102 | as of glibc-2.36 and gcc-12.2).
|
---|
103 | </para>
|
---|
104 |
|
---|
105 | <!-- versions hardcoded in this para, it's a comment on those versions -->
|
---|
106 | <para>If you use this approach, you may discover that more packages than
|
---|
107 | you were expecting use a static library. That was the case with
|
---|
108 | <application>nettle-2.4</application> in its default static-only
|
---|
109 | configuration: It was required by <application>GnuTLS-3.0.19</application>,
|
---|
110 | but also linked into package(s) which used
|
---|
111 | <application>GnuTLS</application>, such as
|
---|
112 | <application>glib-networking-2.32.3</application>.</para>
|
---|
113 |
|
---|
114 | <para>Many packages put some of their common functions into a static
|
---|
115 | library which is only used by the programs within the package and,
|
---|
116 | crucially, the library is <emphasis>not</emphasis> installed as a
|
---|
117 | standalone library. These internal libraries are not a problem – if
|
---|
118 | the package has to be rebuilt to fix a bug or vulnerability, nothing else
|
---|
119 | is linked to them.</para>
|
---|
120 |
|
---|
121 | <para>When BLFS mentions system libraries, it means shared versions of
|
---|
122 | libraries. Some packages such as <xref linkend="firefox"/> and
|
---|
123 | <xref linkend="gs"/> bundle many other libraries in their build tree.
|
---|
124 | The version they ship is often older than the version used in the system,
|
---|
125 | so it may contain bugs – sometimes developers go to the trouble of
|
---|
126 | fixing bugs in their included libraries, other times they do not.</para>
|
---|
127 |
|
---|
128 | <para>Sometimes, deciding to use system libraries is an easy decision.
|
---|
129 | Other times it may require you to alter the system version (e.g. for
|
---|
130 | <xref linkend="libpng"/> if used for <xref linkend="firefox"/>).
|
---|
131 | Occasionally, a package ships an old library and can no longer link to
|
---|
132 | the current version, but can link to an older version. In this case, BLFS
|
---|
133 | will usually just use the shipped version. Sometimes the included library
|
---|
134 | is no longer developed separately, or its upstream is now the same as the
|
---|
135 | package's upstream and you have no other packages which will use it.
|
---|
136 | In those cases, you'll be lead to use the included library even if
|
---|
137 | you usually prefer to use system libraries.</para>
|
---|
138 |
|
---|
139 | </sect2>
|
---|
140 |
|
---|
141 | </sect1>
|
---|