HP-UX Linker and Libraries User's Guide

$ cc -Aa -c +z length.c volume.c mass.c
length.c:
volume.c:
mass.c:
$ ld -b -o libunits.so length.o volume.o mass.o
Once the library is created, ensure that it has read and execute permissions for all users who use
the library. For example, the following chmod command allows read/execute permission for all
users of the libunits.so library:
$ chmod +r+x libunits.so
This library can now be linked with other programs. For example, if you have a C program named
convert.c that calls routines from libunits.so, you can compile and link it with the cc
command:
$ cc -Aa convert.c libunits.so
In PA-32 mode, once the executable is created, the library must not be moved because the absolute
path name of the library is stored in the executable. (In PA-64 and IPF mode, ./libunit.so is
stored in the executable.) For details, see “Shared Library Location (IPF)” (page 102).
For details on linking shared libraries with your programs, see “Determining How to Link Programs
or Libraries (Linker Tasks)” (page 27).
Shared Library Dependencies
You can specify additional shared libraries on the ld command line when creating a shared
library. The created shared library is said to have a dependency on the specified libraries, and
these libraries are known as dependent libraries or supporting libraries. When you load a library
with dependencies, all its dependent libraries are loaded too. For example, suppose you create
a library named libdep.so using the command:
$ ld -b -o libdep.so mod1.o mod2.o -lcurses -lcustom
Thereafter, any programs that load libdep.so - either explicitly with dlopen or shl_load or
implicitly with the dynamic loader when the program begins execution - also automatically load
the dependent libraries libcurses.so and libcustom.so.
There are two additional issues that may be important to some shared library developers:
When a shared library with dependencies is loaded, in what order are the dependent libraries
loaded?
Where are all the dependent libraries placed in relation to other already loaded libraries?
That is, where are they placed in the process's shared library search list used by the dynamic
loader?
The Order in Which Libraries are Loaded (Load Graph)
When a shared library with dependencies is loaded, the dynamic loader builds a load graph to
determine the order in which the dependent libraries are loaded. For example, suppose you create
three libraries - libQ, libD, and libP - using the ld commands below. The order in which the
libraries are built is important because a library must exist before you can specify it as a dependent
library.
$ ld -b -o libQ.so modq.o -lB
$ ld -b -o libD.so modd.o -lQ -lB
$ ld -b -o libP.so modp.o -lA -lD -lQ
The dependency lists for these three libraries are:
libQ depends on libB
libD depends on libQ and libB
libP depends on libA, libD, and libQ
100 Creating and Using Libraries