HP-UX Linker and Libraries User's Guide

Table 20 Differences between archive and shared libraries (continued)
Shared (or dll)ArchiveComparing
Addresses of library subroutines are bound at run time.
Addresses of data in a.out are bound at link time; addresses
of data in shared libraries are bound at run time.
Addresses of library subroutines
and data are resolved at link time.
address binding
Does not contain library routines; instead, contains a linkage
table that is filled in with the addresses of routines and shared
Contains all library routines or
data (external references)
a.out files
library data. An a.out that uses shared libraries is knownreferenced in the program. An
a.out file that does not use
as an incomplete executable, and is almost always much
smaller than a complete executable.shared libraries is known as a
complete executable.
Shared library routines are shared among all processes that
use the library.
Each program has its own copy of
archive library routines.
run time
In Itanium-based systems, some of the system libraries are available both as a shared library and
as an archive library for 32-bit executables in the directory /usr/lib/hpux32/ and for 64-bit
executables in /usr/lib/hpux64/. Archive library file names end with .a whereas shared library
file names end with .so. For example, for 32-bit executables, the archive math library libm is
/usr/lib/hpux32/libm.a and the shared version is /usr/lib/hpux32/libm.so. For
64-bit executables, the archive math library libm is /usr/lib/hpux64/libm.a and the shared
version is /usr/lib/hpux64/libm.so If both shared and archived versions of a library exist,
ld uses the one that it finds first in the default library search path. If both versions exist in the same
directory, ld uses the shared version. For example, compiling the C program prog.c causes cc
to invoke the linker with a command like this:
For 32-bit mode: ld prog.o -lc
For 64-bit mode: ld prog.o -lc
The -lc option instructs the linker to search the C library, hpux32/libc or hpux64/libc, to
resolve unsatisfied references from prog.o. If a shared libc exists
(/usr/lib/hpux32/libc.so or /usr/lib/hpux64/libc.so), ld uses it instead of the
archive libc (/usr/lib/hpux32/libc.a or /usr/lib/hpux64/libc.a). You can,
however, override this behavior and select the archive version of a library with the -a option or
the -l: option. These are described in “Choosing Archive or Shared Libraries with -a (page
36)and “Specifying Libraries with -l and -l:” (page 45) . In addition to the system libraries provided
on HP-UX, you can create your own archive and shared libraries. To create archive libraries,
combine object files with the ar command, as described in “Overview of Creating an Archive
Library” (page 95) . To create shared libraries, use ld to combine object files as described in
“Creating Shared Libraries (page 98). For more information, see “Caution When Mixing Shared
and Archive Libraries (page 118) .
What are Archive Libraries?
An archive library contains one or more object files, and is created with the ar command. When
linking an object file with an archive library, ld searches the library for global definitions that
match with external references in the object file. If a match is found, ld copies the object file
containing the global definition from the library into the a.out file. In short, any routines or data
a program needs from the library are copied into the resulting a.out file. Example For example,
suppose you write a C program that calls printf from the libc library. Figure 6 (page 91) shows
how the resulting a.out file looks if you linked the program with the archive version of libc.
90 Creating and Using Libraries