1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 99.2beta8 (1.46)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>23. Shared Libraries</TITLE>
<META NAME="description" CONTENT="23. Shared Libraries">
<META NAME="keywords" CONTENT="rute">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="rute.css">
<LINK REL="next" HREF="node27.html">
<LINK REL="previous" HREF="node25.html">
<LINK REL="up" HREF="rute.html">
<LINK REL="next" HREF="node27.html">
</HEAD>
<BODY BGCOLOR=#FFFFFF >
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR><TD align=left bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-purchase.html"><FONT COLOR=white>Purchase</FONT></A>
</FONT>
</TD><TD align=center bgcolor="#000000">
<FONT COLOR=white>
Copyright © 2002 Paul Sheer. <A HREF="copying.html"><FONT COLOR=white>Click here for copying permissions.</FONT></A>
</FONT>
</TD><TD align=right bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-home.html"><FONT COLOR=white>Home</FONT></A>
</FONT>
</TD></TR>
<TR><TD colspan=2 align=left bgcolor="#ECEBF4">
<IMG SRC="va-btn-small-light-60.png">
</TD><TD align=right bgcolor="#ECEBF4">
<IMG SRC="sflogo2-steel-60.png">
</TD></TR>
</TABLE><BR>
<!--Navigation Panel-->
<A NAME="tex2html2230"
HREF="node27.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2226"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2220"
HREF="node25.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2228"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2231"
HREF="node27.html">24. Source and Binary</A>
<B> Up:</B> <A NAME="tex2html2227"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2221"
HREF="node25.html">22. Trivial Introduction to</A>
  <B> <A NAME="tex2html2229"
HREF="node1.html">Contents</A></B>
<BR>
<BR>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2232"
HREF="#SECTION002610000000000000000">23.1 Creating DLL <TT>
<FONT COLOR="#0000ff">.so</FONT></TT> Files</A>
<LI><A NAME="tex2html2233"
HREF="#SECTION002620000000000000000">23.2 DLL Versioning</A>
<LI><A NAME="tex2html2234"
HREF="#SECTION002630000000000000000">23.3 Installing DLL <TT>
<FONT COLOR="#0000ff">.so</FONT></TT> Files</A>
</UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION002600000000000000000">
23. Shared Libraries</A>
</H1>
<P>
<A NAME="chap:sharedlib"></A>
<P>
This chapter follows directly from our construction of <I>static</I> <TT>
<FONT COLOR="#0000ff">.a</FONT></TT>
libraries in Chapter <A HREF="node25.html#chap:trivintroc">22</A>. It discusses creation and installation
of <I>Dynamically Linked Libraries</I> (DLLs). Here I show you both so that you
have a good technical overview of how DLLs work on U<SMALL>NIX</SMALL>. You
can then promptly forget everything except <TT>
<FONT COLOR="#0000ff">ldconfig</FONT></TT> and
<TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT> discussed below.
<P>
The <TT>
<FONT COLOR="#0000ff">.a</FONT></TT> library file is good for creating functions that
many programs can include. This practice is called <I>code reuse</I>. But note
how the <TT>
<FONT COLOR="#0000ff">.a</FONT></TT> file is <I>linked into</I> (included) in the executable
<TT>
<FONT COLOR="#0000ff">mytest</FONT></TT> in Chapter <A HREF="node25.html#chap:trivintroc">22</A>. <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT> is enlarged by the size of
<TT>
<FONT COLOR="#0000ff">libsimple_math.a</FONT></TT>. When hundreds of programs use
the same <TT>
<FONT COLOR="#0000ff">.a</FONT></TT> file, that code is effectively duplicated all
over the file system. Such inefficiency was deemed unacceptable
long before L<SMALL>INUX</SMALL>, so library files were invented that only link
with the program when it runs--a process known as
<I>dynamic</I> linking. Instead of <TT>
<FONT COLOR="#0000ff">.a</FONT></TT> files, similar <TT>
<FONT COLOR="#0000ff">.so</FONT></TT>
(<TT>
<FONT COLOR="#0000ff">s</FONT></TT><I>hared</I> <TT>
<FONT COLOR="#0000ff">o</FONT></TT><I>bject</I>) files live in <TT>
<FONT COLOR="#0000ff">/lib/</FONT></TT>
and <TT>
<FONT COLOR="#0000ff">/usr/lib/</FONT></TT> and are automatically linked to a program when it
runs.
<P>
<H1><A NAME="SECTION002610000000000000000">
23.1 Creating DLL <TT>
<FONT COLOR="#0000ff">.so</FONT></TT> Files</A>
</H1>
<P>
<A NAME="sec:sharedobj"></A>
<P>
Creating a DLL requires several changes to the <TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>
on page <A HREF="node25.html#page:libmakefile"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A>:
<P>
<A NAME="page:sharemakefile"></A>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>OBJS = simple_math_sqrt.o simple_math_pow.o</code><br>
<code>LIBNAME = simple_math</code><br>
<code>SONAME = libsimple_math.so.1.0.0</code><br>
<code>SOVERSION = libsimple_math.so.1.0</code><br>
<code>CFLAGS = -Wall</code><br>
<code> </code><br>
<code>all: lib$(LIBNAME).so mytest</code><br>
<code> </code><br>
<code>mytest: lib$(LIBNAME).so mytest.o</code><br>
<code> gcc $(CFLAGS) -o $@ mytest.o -L. -l${LIBNAME}</code><br>
<code> </code><br>
<code>lib$(LIBNAME).so: $(OBJS)</code><br>
<code> gcc -shared $(CFLAGS) $(OBJS) -lc -Wl,-soname -Wl,$(SOVERSION) \</code><br>
<code> -o $(SONAME) && \</code><br>
<code> ln -sf $(SONAME) $(SOVERSION) && \</code><br>
<code> ln -sf $(SONAME) lib$(LIBNAME).so</code><br>
<code> </code><br>
<code>.c.o:</code><br>
<code> gcc -fPIC -DPIC $(CFLAGS) -c -o $*.o $<</code><br>
<code> </code><br>
<code>clean:</code><br>
<code> rm -f *.o *.a *.so mytest</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">-shared</FONT></TT> option to <TT>
<FONT COLOR="#0000ff">gcc</FONT></TT> builds our shared
library. The <TT>
<FONT COLOR="#0000ff">-W</FONT></TT> options are linker options that set the version
number of the library that linking programs will load at runtime. The
<TT>
<FONT COLOR="#0000ff">-fPIC -DPIC</FONT></TT> means to generate
<I>position-independent code</I>,
that is, code suitable for dynamic linking.
<P>
After running <TT>
<FONT COLOR="#0000ff">make</FONT></TT> we have
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red size="-2">
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue size="-2">
<code>lrwxrwxrwx 1 root root 23 Sep 17 22:02 libsimple_math.so -> libsimple_math.so.1.0.0</code><br>
<code>lrwxrwxrwx 1 root root 23 Sep 17 22:02 libsimple_math.so.1.0 -> libsimple_math.so.1.0.0</code><br>
<code>-rwxr-xr-x 1 root root 6046 Sep 17 22:02 libsimple_math.so.1.0.0</code><br>
<code>-rwxr-xr-x 1 root root 13677 Sep 17 22:02 mytest</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION002620000000000000000">
23.2 DLL Versioning</A>
</H1>
<P>
You may observe that our three <TT>
<FONT COLOR="#0000ff">.so</FONT></TT> files are
similar to the many files in <TT>
<FONT COLOR="#0000ff">/lib/</FONT></TT> and <TT>
<FONT COLOR="#0000ff">/usr/lib/</FONT></TT>. This
complicated system of linking and symlinking is part of the process
of <I>library versioning</I>. Although generating a DLL is out of the
scope of most system admin tasks, library versioning is important to
understand.
<P>
DLLs have a problem. Consider a DLL that is outdated or buggy:
simply overwriting the DLL file with an updated file will affect all the
applications that use it. If these applications rely on certain
behavior of the DLL code, then they will probably crash with the
fresh DLL. U<SMALL>NIX</SMALL> has elegantly solved this problem by allowing
multiple versions of DLLs to be present simultaneously. The
programs themselves have their required version number built into
them. Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ldd mytest</code><br>
</FONT></TD></TR></TABLE><P>
which will show the DLL files that <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT> is scheduled to link
with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>libsimple_math.so.1.0 => ./libsimple_math.so.1.0 (0x40018000)</code><br>
<code>libc.so.6 => /lib/libc.so.6 (0x40022000)</code><br>
<code>/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)</code><br>
</FONT></TD></TR></TABLE><P>
At the moment, we are interested in <TT>
<FONT COLOR="#0000ff">libsimple_math.so.1.0</FONT></TT>.
Note how it matches the <TT>
<FONT COLOR="#0000ff">SOVERSION</FONT></TT> variable in the
<TT>
<FONT COLOR="#0000ff">Makefile</FONT></TT>. Note also how we have chosen our symlinks.
We are effectively allowing <TT>
<FONT COLOR="#0000ff">mytest</FONT></TT> to link with any future
<TT>
<FONT COLOR="#0000ff">libsimple_math.so.1.0.</FONT></TT><I>?</I> (were our <TT>
<FONT COLOR="#0000ff">simple_math</FONT></TT> library
to be upgraded to a new version) purely because of the way we have chosen
our symlinks. However, it will not link with any
library <TT>
<FONT COLOR="#0000ff">libsimple_math.so.1.1.</FONT></TT><I>?</I>, for example. As developers of
<TT>
<FONT COLOR="#0000ff">libsimple_math</FONT></TT>, we are deciding that libraries of a different
<I>minor</I> <FONT COLOR="#ffa500">[For this example we are considering libraries to be
named
<TT>
<FONT COLOR="#0000ff">lib</FONT></TT><I>name</I><TT>
<FONT COLOR="#0000ff">.so.</FONT></TT><I>major</I><TT>
<FONT COLOR="#0000ff">.</FONT></TT><I>minor</I><TT>
<FONT COLOR="#0000ff">.</FONT></TT><I>patch</I>]</FONT>version number will be incompatible, whereas libraries of a different
<I>patch</I> level will not be incompatible.
<P>
We could also change <TT>
<FONT COLOR="#0000ff">SOVERSION</FONT></TT> to
<TT>
<FONT COLOR="#0000ff">libsimple_math.so.1</FONT></TT>. This would effectively be saying that
future libraries of different minor version numbers are compatible; only
a change in the major version number would dictate incompatibility.
<P>
<H1><A NAME="SECTION002630000000000000000">
23.3 Installing DLL <TT>
<FONT COLOR="#0000ff">.so</FONT></TT> Files</A>
</H1>
<P>
If you run <TT>
<FONT COLOR="#0000ff">./mytest</FONT></TT>, you will be greeted with an <TT>
<FONT COLOR="#0000ff">error
while loading shared libraries</FONT></TT> message. The reason is that the
dynamic linker does not search the current directory for <TT>
<FONT COLOR="#0000ff">.so</FONT></TT>
files. To run your program, you will have to install your library:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>mkdir -p /usr/local/lib</code><br>
<code>install -m 0755 libsimple_math.so libsimple_math.so.1.0 \</code><br>
<code> libsimple_math.so.1.0.0 /usr/local/lib</code><br>
</FONT></TD></TR></TABLE><P>
Then, edit the <TT>
<FONT COLOR="#0000ff">/etc/ld.so.conf</FONT></TT> file and add a line
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>/usr/local/lib</code><br>
</FONT></TD></TR></TABLE><P>
Then, reconfigure your libraries with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ldconfig</code><br>
</FONT></TD></TR></TABLE><P>
Finally, run your program with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"</code><br>
<code>./mytest</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<A NAME="page:ldlibpathvar"></A><TT>
<FONT COLOR="#0000ff">ldconfig</FONT></TT> configures all libraries on the system.
It recreates appropriate symlinks (as we did) and rebuilds a lookup
cache. The library directories it considers are <TT>
<FONT COLOR="#0000ff">/lib</FONT></TT>,
<TT>
<FONT COLOR="#0000ff">/usr/lib</FONT></TT>, and those listed in <TT>
<FONT COLOR="#0000ff">/etc/ld.so.config</FONT></TT>.
The <TT>
<FONT COLOR="#0000ff">ldconfig</FONT></TT> command should be run automatically when the
system boots and manually whenever libraries are installed or upgraded.
<P>
The <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT> environment variable is
relevant to every executable on the system and similar to the
<TT>
<FONT COLOR="#0000ff">PATH</FONT></TT> environment variable. <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT>
dictates what directories should be searched for
library files. Here, we appended <TT>
<FONT COLOR="#0000ff">/usr/local/lib</FONT></TT> to the
search path in case it was missing. Note that even with
<TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT> unset, <TT>
<FONT COLOR="#0000ff">/lib</FONT></TT> and <TT>
<FONT COLOR="#0000ff">/usr/lib</FONT></TT>
will always be searched.
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html2230"
HREF="node27.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2226"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2220"
HREF="node25.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2228"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2231"
HREF="node27.html">24. Source and Binary</A>
<B> Up:</B> <A NAME="tex2html2227"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2221"
HREF="node25.html">22. Trivial Introduction to</A>
  <B> <A NAME="tex2html2229"
HREF="node1.html">Contents</A></B>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|