Penguin
Blame: SharedLibraryNotes
EditPageHistoryDiffInfoLikePages
Annotated edit history of SharedLibraryNotes version 5, including all changes. View license author blame.
Rev Author # Line
4 SamJansen 1 !!! Examining SharedLibrary dependencies on [Unix]
2
3 On many [Unix] systems, you can use the ldd(1) command to see which shared libraries a program is linked against:
4
5 <verbatim>
6 $ ldd /sbin/mke2fs
7 libext2fs.so.2 => /lib/libext2fs.so.2 (0x40023000)
8 libcom_err.so.2 => /lib/libcom_err.so.2 (0x40037000)
9 libe2p.so.2 => /lib/libe2p.so.2 (0x40039000)
10 libuuid.so.1 => /lib/libuuid.so.1 (0x4003e000)
11 libc.so.6 => /lib/libc.so.6 (0x40041000)
12 /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
13 </verbatim>
14
15 Trying this on a statically linked program:
16
17 <verbatim>
18 $ ldd /sbin/init
19 not a dynamic executable
20 </verbatim>
21
22 On [Darwin], the corresponding program is called <tt>otool</tt>:
23
24 <verbatim>
25 $ otool -L /bin/zsh
26 /bin/zsh:
27 /usr/lib/libncurses.5.dylib (compatibility version 5.0.0, current version 5.0.0)
28 /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 60.0.0)
29 </verbatim>
30
31 !!! Creating a SharedLibrary
32
33 Assuming the source is in the files <tt>foo.c</tt>, <tt>bar.c</tt>, <tt>baz.c</tt>, you call [GCC] like so:
34
35 <verbatim>
36 gcc -shared -Wl,-soname,libfoo.so -o libfoo.so.1.0.1 foo.c bar.c baz.c -lquux
37 </verbatim>
38
39 This creates a SharedLibrary file <tt>libfoo.so.1.0.1</tt>, with shared object name ("soname") <tt>libfoo.so</tt>, dependent on <tt>libquux</tt>. To make <tt>libfoo</tt> usable, you need to put it someplace ldconfig(8) will find it, and SymLink it to <tt>libfoo.so.1</tt> and <tt>libfoo.so</tt>.
40
41 So long as you don't change the call signature of one of the functions (or what they do, in a way that would break programs), you can just bump the minor version of the library and programs using the SharedLibrary will just work. If you do change call signatures on existing functions or change their semantics, bump the version, try to recompile the programs, and check that they work correctly once they pass. Another way to make it usable is to put it in a directory refered to in your <tt>LD_LIBRARY_PATH</tt> EnvironmentVariable.
42
43 You may wish to use <tt>-Wl,-O1</tt> when linking too, it will optimise the hashtables used for symbol lookup making the startup time faster.
44
45 !!! Creating static shared libraries
46
47 In some cases you may not want your shared library to have any extra dependencies. In such a case you want your shared library to have its dependencies statically linked in. Even a simple shared library will have a couple of dependences:
48
49 <verbatim>
50 ldd ./libsam.so
51 libc.so.6 => /lib/libc.so.6 (0x0000002a95675000)
52 /lib64/ld-linux-x86-64.so.2 (0x000000552aaaa000)
53 </verbatim>
54
55 The obvious way to statically link these dependencies would be:
56
57 <verbatim>
58 gcc -shared -static -o libsam.so sam.c
59 </verbatim>
60
61 This will either not work, or give an error message like:
62
63 <verbatim>
64 gcc: -shared and -static|pie|fPIE|fpie|fno-PIC|fno-pic|nopie are incompatible
65 </verbatim>
66
67 However, it is possible to directly tell the linker we want static linking, and then magic will happen:
68
69 <verbatim>
70 gcc -shared -Wl,-static -o libsam.so sam.c
71
72 ldd ./libsam.so
73 statically linked
74 </verbatim>
75
76 ''libsam.so'' is still a valid shared library, it just lacks any external dependency. In some cases you might need the command-line option ''-static-libgcc'' for this to work.
77
78 !!! Creating a SharedLibrary using libtool(1)
79
80 libtool(1) is a package designed to assist with the creation of shared libraries. See the libtool(1) node for more information (eventually) about this program
81
82 One of the advantages of libtool is that it will use the correct linker options on different platforms, instead of you having to learn them all and putting them in your MakeFile/configure script. (For example [GCC] uses </tt>-shared</tt>, <tt>-fPIC</tt>, <tt>-soname</tt> for certain options, [Solaris]'s [CC] uses <tt>-G</tt>, <tt>-KPIC</tt>, and <tt>-h</tt> respectively for those same options).
83
84 If you have a <tt>.c</tt> file you wish to include in a library with libtool, you need to make a <tt>.lo</tt> file for it:
85
86 <verbatim>
87 libtool --mode=compile gcc -c trace.c -o libtrace.lo
88 </verbatim>
89
90 Once you have a .lo file, you need to make a .la file
91
92 <verbatim>
93 LDLIBS=-lpcap -lz
94 libtool --mode=link gcc -o libtrace.la libtrace.lo -rpath /usr/local/wand/lib \
95 $(LDLIBS) \
96 -version-info 1:2:0
97 </verbatim>
98
99 One nice feature of libtool is that you can specify other shared libraries that this one will depend upon (in the <tt>LD_LIBS</tt> EnvironmentVariable above), and this will be included in the linker script -- any package linking against <tt>libtrace</tt> in the above example will automatically link against <tt>libpcap</tt> and <tt>libz</tt> as well.
100
101 To install the libraries, you use libtool once more:
102
103 <verbatim>
104 libtool --mode=install install -c libtrace.la /usr/local/wand/lib/libtrace.la
105 libtool --finish /usr/local/wand/lib/
106 </verbatim>
107
108 As a final step, I make sure to install my header files somewhere useful!
109
110 <verbatim>
111 cp ../include/libtrace.h /usr/local/wand/include/libtrace.h
112 </verbatim>
113
114 There are excellent docs on using libtool supplied with the program.
115
116 !!! Examining symbols in a shared library
117
118 Using nm(1) on dynamic libraries requires the <tt>--dynamic</tt> option:
119
120 <verbatim>
121 nm /lib/libc.so.6 # doesn't work
122 nm --dynamic /lib/libc.so.6 # does work
123 </verbatim>
5 CraigBox 124
125 !!!Help, I moved my library directory and now nothing runs!
126
127 A misplaced / led me to moving everything from / to '/etc/old' - when you can't find the library loader, you can't even run bash builtins. Get around it like so:
128
129 <pre>
130 /etc/old/lib/ld-linux.so.2 /etc/old/bin/mv /etc/old/bin /
131 </pre>
132
4 SamJansen 133
134 !!! See also
135
136 * Ulrich Drepper's [How to write shared libraries | http://people.redhat.com/drepper/dsohowto.pdf]
137 * Mike Hearn's [Writing shared libraries | http://navi.cx/~mike/writing-shared-libraries.html]