HP Fortran Programmer's Guide (September 2007)
Compiling and linking
Linking HP Fortran programs
Chapter 2 81
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
+sharedlibIO77 options).
To force the linker to select archive libraries, specify the -Wl,-a,archive option on the f90
command line. f90 passes 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.f90 and 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 -a option, see the ld(1) man page. For more information
about shared libraries, see “Creating shared libraries” on page 87.
Library search rules
When you use the -l option to specify the name of a library, the linker searches for the library
in the directories specified by the LPATH environment variable. The f90 command 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 setting LPATH yourself; see “LPATH environment
variable” on page 95.
Alternatively, you can use the -L
directory
option to direct the linker to search
directory
before 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_libs and then looking in /opt/fortran90/lib and /usr/lib.