HP Fortran Programmer Guide HP-UX 11i v1, HP-UX 11i v2, and HP-UX 11i v3 (B3908-90032,December 2012)

Linking to shared libraries
Many HP Fortran libraries as well as HP-UX libraries exist in both shared and archive versions, as
indicated by the library extension name (.sl or.aor .slon Itanium). For example, there are
both shared and archive versions of the HP Fortran runtime library, /usr/lib/libcl.sl and
/usr/lib/libcl.a.
The difference between a shared library and an archive library is that the linker does not actually
link the code in a shared library with your program. Instead, any references that your program
makes to entities in the shared library are resolved at load-time, when the library is loaded into
the executable program’s address space. By contrast, code in the archive library is copied to the
executable program file.
The advantages of linking shared libraries are:
The executable is smaller than it would be if linked with an archive file because the executable
file is incomplete—it doesn’t include code from the library.
Using shared libraries ensures that you always get the most recent version of the library. If
you link with an archive version, you get the version that was available at link-time. If, later
on, you want a more recent version of the library, you must re-link your program with that
library.
The disadvantage of linking with a shared library is that it creates a dependency between the
library and the program; both the shared library and the program must always be installed together
on the same system. By contrast, linking with an archive library makes the executable program
independent of the library to which it was linked. Also, programs that make frequent calls to library
routines may run more slowly when linked to shared libraries.
By default, the linker selects the shared version of a library, if one is available; otherwise, it selects
the archive version.
NOTE: For libF90, libU77, and libIO77(Itanium® only), archive libraries are selected by
default (see +sharedlibF90, +sharedlibU77, and +sharedlibIO77options).
To force the linker to select archive libraries, specify the -Wl,-a,archiveoption on the
f90command line. f90passes the arguments to the -Wl option (-a and archive) to the linker.
This option must appear before the names of any libraries also specified on the command line.
The following command line compiles prog.f90and links it with the archive versions of the default
libraries as well as with the math library (as specified by the-lm option):
$ f90 -Wl,-a,archive prog.f90 -lm
For information about the linker’s -aoption, see the ld(
1) man page. For more information about shared libraries, see “Creating shared libraries” on
page 90.
Library search rules
When you use the -loption to specify the name of a library, the linker searches for the library in
the directories specified by the LPATHenvironment variable. The f90command sets this variable
so that the linker looks first in /opt/fortran90/lib, then in/usr/lib . You can specify another
directory to search by settingLPATHyourself; see “LPATH environment variable” on page 98.
Alternatively, you can use the -Ldirectoryoption to direct the linker to search directorybefore
it looks anywhere else to resolve references. For example, the following command line:
$ f90 -L/my_libs prog.f90 -lstuff
causes the linker to search for libraries (including libstuff.sl and libstuff.a), starting
with the directory /my_libsand then looking in /opt/fortran90/lib and /usr/lib.
52 Compiling and linking