Linking with -b

The -boption 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-boption must be used with the ldcommand; you cannot use thef90 command to create a shared library. Also, the object files specified on theld command line must consist of PIC; that is, they must have been created with either +pic=shortor +pic=long.

The following command line links the object filesx.o , y.o, and z.ointo a shared library, named my_lib.sl:

$ ld -b -o my_lib.sl x.o y.o z.o

Note that thisld command line is much simpler than the ld command line required to link an executable file (for example, see “Linking with f90 vs. ld” (page 50).

Examples

Example 10 Example 2-6 hi.f90

SUBROUTINE say_hi()

PRINT *, 'Hi!'

END SUBROUTINE say_hi

Example 11 Example 2-7 bye.f90

SUBROUTINE say_bye()

PRINT *, 'Bye!'

END SUBROUTINE say_bye

Example 12 Example 2-8 greet.f90

PROGRAM main

CALL say_hi()

CALL say_bye()

END PROGRAM main

The following command line creates the PIC object files (the -coption suppresses linking):

$ f90 -c +pic=short bye.f90 hi.f90

The next command line links the object files into the shared library:

$ ld -b -o my_lib.sl bye.o hi.o

The last command line compiles the source file greet.f90 and links the object code with the shared library to produce the executable program a.out:

$ f90 greet.f90 my_lib.sl

The following is the output from a sample run of the executable program:

$ a.out

Hi!

Bye!

Using the C preprocessor

You can use the f90 command to pass source files to the C preprocessor (cpp) before they are compiled. If the source files contain C preprocessor directives, cpp will act on the directives, modifying the source text accordingly. The f90 driver will then pass the preprocessed source text to the compiler. Adding cpp directives to program source files and having the cpp command preprocess them is a convenient way to maintain multiple versions of a program—for example, a debugging version and a production version—in one set of files.

60 Compiling and linking