Table 20 Differences between archive and shared libraries (continued)

Comparing

Archive

Shared (or dll)

address binding

Addresses of library subroutines

Addresses of library subroutines are bound at run time.

 

and data are resolved at link time.

Addresses of data in a.out are bound at link time; addresses

 

 

of data in shared libraries are bound at run time.

 

 

 

a.out files

Contains all library routines or

Does not contain library routines; instead, contains a linkage

 

data (external references)

table that is filled in with the addresses of routines and shared

 

referenced in the program. An

library data. An a.out that uses shared libraries is known

 

a.out file that does not use

as an incomplete executable, and is almost always much

 

shared libraries is known as a

smaller than a complete executable.

 

complete executable.

 

run time

Each program has its own copy of

 

archive library routines.

Shared library routines are shared among all processes that use the library.

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 -lcoption 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 -aoption 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