Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
SharedLibraryNotes
Edit
PageHistory
Diff
Info
LikePages
!!! Examining SharedLibrary dependencies on [Unix] On many [Unix] systems, you can use the ldd(1) command to see which shared libraries a program is linked against: <verbatim> $ ldd /sbin/mke2fs libext2fs.so.2 => /lib/libext2fs.so.2 (0x40023000) libcom_err.so.2 => /lib/libcom_err.so.2 (0x40037000) libe2p.so.2 => /lib/libe2p.so.2 (0x40039000) libuuid.so.1 => /lib/libuuid.so.1 (0x4003e000) libc.so.6 => /lib/libc.so.6 (0x40041000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) </verbatim> Trying this on a statically linked program: <verbatim> $ ldd /sbin/init not a dynamic executable </verbatim> On [Darwin], the corresponding program is called <tt>otool</tt>: <verbatim> $ otool -L /bin/zsh /bin/zsh: /usr/lib/libncurses.5.dylib (compatibility version 5.0.0, current version 5.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 60.0.0) </verbatim> !!! Creating a SharedLibrary Assuming the source is in the files <tt>foo.c</tt>, <tt>bar.c</tt>, <tt>baz.c</tt>, you call [GCC] like so: <verbatim> gcc -shared -Wl,-soname,libfoo.so -o libfoo.so.1.0.1 foo.c bar.c baz.c -lquux </verbatim> 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>. 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. 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. !!! Creating static shared libraries 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: <verbatim> ldd ./libsam.so libc.so.6 => /lib/libc.so.6 (0x0000002a95675000) /lib64/ld-linux-x86-64.so.2 (0x000000552aaaa000) </verbatim> The obvious way to statically link these dependencies would be: <verbatim> gcc -shared -static -o libsam.so sam.c </verbatim> This will either not work, or give an error message like: <verbatim> gcc: -shared and -static|pie|fPIE|fpie|fno-PIC|fno-pic|nopie are incompatible </verbatim> However, it is possible to directly tell the linker we want static linking, and then magic will happen: <verbatim> gcc -shared -Wl,-static -o libsam.so sam.c ldd ./libsam.so statically linked </verbatim> ''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. !!! Creating a SharedLibrary using libtool(1) 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 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). 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: <verbatim> libtool --mode=compile gcc -c trace.c -o libtrace.lo </verbatim> Once you have a .lo file, you need to make a .la file <verbatim> LDLIBS=-lpcap -lz libtool --mode=link gcc -o libtrace.la libtrace.lo -rpath /usr/local/wand/lib \ $(LDLIBS) \ -version-info 1:2:0 </verbatim> 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. To install the libraries, you use libtool once more: <verbatim> libtool --mode=install install -c libtrace.la /usr/local/wand/lib/libtrace.la libtool --finish /usr/local/wand/lib/ </verbatim> As a final step, I make sure to install my header files somewhere useful! <verbatim> cp ../include/libtrace.h /usr/local/wand/include/libtrace.h </verbatim> There are excellent docs on using libtool supplied with the program. !!! Examining symbols in a shared library Using nm(1) on dynamic libraries requires the <tt>--dynamic</tt> option: <verbatim> nm /lib/libc.so.6 # doesn't work nm --dynamic /lib/libc.so.6 # does work </verbatim> !!!Help, I moved my library directory and now nothing runs! 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: <pre> /etc/old/lib/ld-linux.so.2 /etc/old/bin/mv /etc/old/bin / </pre> !!! See also * Ulrich Drepper's [How to write shared libraries | http://people.redhat.com/drepper/dsohowto.pdf] * Mike Hearn's [Writing shared libraries | http://navi.cx/~mike/writing-shared-libraries.html]
2 pages link to
SharedLibraryNotes
:
SharedLibrary
CNotes