NOTE: The compiler generates a.modfile for each file that defines a Fortran module. It also reads the .modfiles when compiling source files that use modules. Do not specify .mod files on the command line. If you do, the compiler will pass them to the linker, which will try (and fail) to link them into the executable. For more information about .mod files, see “Compiling programs with modules” (page 54).

Linking HP Fortran programs

This section discusses how to link object files and covers the following topics:

The advantages of using the f90 command for linking as well as for compiling

How to link libraries, including shared libraries

How to establish the search rules used by the linker when it looks for libraries

For more information about the linker, see Programming on HP-UXand to the ld (1) man page.

Linking with f90 vs. ld

By default, the f90 command both compiles and links, producing an executable program. You can modify this behavior with the -coption, which causes f90to compile only, writing the object files (if the compilation is successful) in the current working directory. If the command line contains object files only, f90passes them to the linker (ld) for linking into the executable program. In other words, you can use the f90command to compile and link in one command line or in separate command lines. You do not need to invoke the ld command separately.

In fact, we recommend that you use the f90 command whenever you link HP Fortran object files and that you use the same command line for linking as for compiling.

When you use the f90 command to compile and link in the same command line, the driver passes certain information—search paths, library names, and options—to the linker. If you use the ld command to link separately, you must specify this same information on theld command line. Not doing so can cause the link to fail. Using the samef90 command line to link as you use to compile avoids the problem of passing insufficient or incorrect information to the linker.

To see what information f90passes to the linker, compile with the-voption (verbose mode). Here is the hello.f90 program (listed in “Compiling with the f90 command” (page 18)) compiled in verbose mode. The lines are numbered for the convenience of referencing:

1 $ f90 -v hello.f90

2 /opt/fortran90/lbin/f90com -cm -w90 -nbs -auto

-WB -hp\”-Oq00,al,ag,cn,Lm,sz,Ic,vo,lc,mf,po,es,rs,sp, in,vc,pi,fa,pe,Rr,Fl,pv,pa,nf,cp,lx,st,ap,Pg, ug,lu,dp,fs,bp,wp\!\” hello.f90

3hello.f90

4program MAIN

5external subroutine HELLO

67 Lines Compiled

7LPATH is: /opt/fortran90/lib/pa1.1:/usr/lib/pa1.1: /opt/fortran90/lib:/usr/lib:/opt/langtools/lib

8/usr/ccs/bin/ld -x /opt/langtools/lib/crt0.o hello.o /opt/fortran90/lib/libF90.a -lcl -lc -lisamstub

Line 1 is the f90 command line.

Line 2 is the information f90 passes to the compiler, including the full pathname of the compiler, the name of the source file (hello.f90), and the internal names of the option settings as determined by the defaults and the f90command line.

Lines 3 - 6 show the progress of the compilation; line 6 indicates that the compilation was successful.

Line 7 displays the value to which f90 has defined the LPATH environment variable. If you use the ld command to link hello.f90, you must defineLPATH on the command line before invoking the linker. See “LPATH environment variable” (page 65).

50 Compiling and linking