HP Fortran Programmer's Guide (B3908-90031; September 2011)

Compiling and linking
Special-purpose compilations
Chapter 2 91
Although compiling with either +pic=short or +pic=long will generate PIC, in general you should use
the +pic=short option. If the linker issues an error message saying that the number of referenced symbols
in the shared library exceeds its limit, recompile with +pic=long, which will cause the compiler to allocate
space for a longer symbol table.
The +pic=no is the default, which causes the compiler to generate absolute code, such as you would want
for executable programs.
The following command line creates three object files—x.o, y.o, and z.o; the code in each file will be
PIC:
$ f90 -c +pic=short x.f90 y.f90 z.f90
For more information about the +pic option, see the HP Fortran Programmers Reference.
Linking with -b
The -b option is a linker option. It causes the linker to bind PIC object files into a shared library, instead of
creating a normal executable file. The -b option must be used with the ld command; you cannot use the
f90 command to create a shared library. Also, the object files specified on the ld command line must
consist of PIC; that is, they must have been created with either +pic=short or +pic=long.
The following command line links the object files x.o, y.o, and z.o into a shared library, named
my_lib.sl:
$ ld -b -o my_lib.sl x.o y.o z.o
Note that this ld command line is much simpler than the ld command line required to link an executable
file (for example, see “Linking with f90 vs. ld” on page 78).
Examples
This section shows an example of how to create and link to a shared library. The shared library will consist
of PIC object files compiled from the source files, hi.f90 and bye.f90. The library, my_lib.sl, will be
linked to the executable program compiled from greet.f90. The code for three HP Fortran source files
follows:
Example 2-6 hi.f90
SUBROUTINE say_hi()
PRINT *, 'Hi!'
END SUBROUTINE say_hi
Example 2-7 bye.f90
SUBROUTINE say_bye()
PRINT *, 'Bye!'
END SUBROUTINE say_bye