HP Fortran Programmer's Guide (March 2010)

Compiling and linking
Special-purpose compilations
Chapter 294
Compiling with +pic
The +pic option causes the compiler to generate Position- Independent Code (PIC) for use
in a shared library. PIC contains no absolute addresses and can therefore be placed anywhere
in a process’s address space without addresses having to be relocated. This characteristic of
PIC makes it shareable by multiple processes.
The syntax of the +pic option is:
+pic={short|long|no}
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 Programmer’s 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 81).
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: