The syntax of a simple compiler command invoked at a shell prompt is:
f77 [options] files... invokes the Fortran 77 compiler |
f90 [options] files... invokes the Fortran 90 compiler |
Here files... is one or more Fortran source file names ending in .f, .F, .f90, .F90, or .for; options is one or more of the compiler option flags. (Files with names ending in a .f90 or .F90 extension are "free-format" Fortran 90 source files recognized only by the f90 compiler.)
In the example below, f90 is used to compile two source files to produce an executable file named growth with runtime debugging enabled:
demo% f90 -g -o growth growth.f fft.f90
In the previous example, the compiler will automatically generate the loader object files, growth.o and fft.o, and then invoke the system linker to create the executable program on the file growth.
After compilation, the object files, growth.o and fft.o, will remain. This convention permits easy relinking and recompilation of files.
If the compilation fails, you will receive a message for each error. No .o files are generated for those source files with errors, and no executable program is written
The suffix extension attached to file names appearing on the command-line determine how the compiler will process the file. File names with a suffix extension other than one of those listed below, or without an extension, are passed to the linker.
Table 2-1 File Name Suffixes Recognized by Sun Fortran Compilers
Suffix |
Language |
Action |
---|---|---|
.f |
Fortran 77 or Fortran 90 fixed-format |
Compile Fortran source files, put object files in current directory; default name of object file is that of the source but with .o suffix. |
.f90 |
Fortran 90 free-format |
Same action as .f (f90 only) |
.for |
Fortran 77 |
Same action as .f. |
.F |
Fortran 77 or Fortran 90 fixed-format |
Apply the Fortran (or C) preprocessor to the Fortran 77 source file before compilation. |
.F90 |
Fortran 90 free-format |
Apply the Fortran (or C) preprocessor to the Fortran 90 free-format source file before Fortran compiles it. |
.s |
Assembler |
Assemble source files with the assembler. |
.S |
Assembler |
Apply the C preprocessor to the assembler source file before assembling it. |
.il |
Inline expansion |
Process template files for inline expansion. The compiler will use templates to expand inline calls to selected routines. (Template files are special assembler files; see the inline(1) man page.) |
.o |
Object files |
Pass object files through to the linker. |
.a,.so, .so.n |
Libraries |
Pass names of libraries to the linker. .a files are static libraries, .so and .so.n files are dynamic libraries. |
Fortran 90 free-format is described in Appendix C of this manual.
The Fortran compilers will accept multiple source files on the command line. A single source file, also called a compilation unit, may contain any number of procedures (main program, subroutine, function, block data, module, and so on). There are advantages for organizing an application with one procedure per file, as there are for gathering procedures that work together into a single file. Some of these are described in the Sun Fortran Programming Guide.
Both f77 and f90 support two source file preprocessors, fpp and cpp. Either can be invoked by the compiler to expand source code "macros" and symbolic definitions prior to compilation. The compilers will use fpp by default; the -xpp=cpp option changes the default from fpp to cpp. (See also the discussion of the -Dname option).
fpp is a source preprocessor specifically for the Fortran language. See the fpp(1) man page, and the fpp white paper in the READMEs directory for details. It is invoked by default by f77 on files with a .F extension, and by f90 on files with a .F or .F90 extension.
The cpp program is the C language preprocessor. See cpp(1). Use of fpp over cpp is recommended.
You can compile and link in separate steps. The -c option compiles source files and generates .o object files, but does not create an executable. Without the -c option the compiler will invoke the linker. By splitting the compile and link steps in this manner, a complete recompilation is not needed just to fix one file, as shown in the following example:
Compile one file and link with others in separate steps:
demo% f77 -c file1.f (Make new object file) demo% f77 -o prgrm file1.o file2.o file3.o (Make executable file)
Be sure that the link step lists all the object files needed to make the complete program. If any object files are missing from this step, the link will fail with undefined external reference errors (missing routines).
Ensuring a consistent choice of compiling and linking options is critical whenever compilation and linking are done in separate steps. Compiling any part of a program with any of the following options requires linking with the same options:
-a, -autopar, -Bx, -dbl, -fast, -G, -Lpath, -lname, -mt, -nolib, -norunpath, -p, -pg, -xlibmopt, -xlic_lib=name, -xprofile=p
Example: Compiling sbr.f with -a and smain.f without it, then linking in separate steps (-a invokes tcov old-style profiling):
demo% f77 -c -a sbr.f demo% f77 -c smain.f demo% f77 -a sbr.o smain.o link step; passes -a to the linker
Also, a number of options require that all source files be compiled with that option, including the link step. These include:
-autopar, -cg92, -dx, -dalign, -explicitpar, -f, -misalign, -native, -parallel, -pentium, -r8, -xarch=a, -xcache=c, -xchip=c, -xF, -xtarget=t, -ztext
As a general rule, if any of the object files that make up a program were compiled with f90, then the final link step must be done with f90. Use f77 to produce the executable file only if none of the .o object files were compiled with f90.
Any arguments on the command-line that the compiler does not recognize are interpreted as being possibly linker options, object program file names, or library names.
The basic distinctions are:
Unrecognized options (with a -) generate warnings.
Unrecognized non-options (no -) generate no warnings. However, they are passed to the linker and if the linker does not recognize them, they generate linker error messages.
demo% f77 -bit move.f <- -bit is not a recognized f77 option f77: Warning: Option -bit passed to ld, if ld is invoked, ignored otherwise move.f: MAIN move: demo% f77 fast move.f <- The user meant to type -fast move.f: MAIN move: ld: fatal: file fast: cannot open file; errno=2 ld: fatal: File processing errors. No output written to a.out
Note that in the first example, -bit is not recognized by f77 and the option is passed on to the linker (ld), who tries to interpret it. Because single letter ld options may be strung together, the linker sees -bit as -b -i -t, which are all legitimate ld options! This may (or may not) be what the user expects, or intended.
In the second example, the user intended to type the f77/f90 option -fast but neglected the leading dash. The compiler again passes the argument to the linker which, in turn, interprets it as a file name.
These examples indicate that extreme care should be observed when composing compiler command lines!
f90 automatically creates module information files for each MODULE declaration encountered in the source files, and searches for modules referenced by a USE statement. All the modules appearing in a source file are compiled into a single file with the primary name of the MODULE and .mod suffix. For example, f90 generates the module information file list.mod for the MODULE list unit found on file mysrc.f90 .
The compiler searches the current directory for module files referenced in USE statements. Modules files must be compiled before compiling any source file referencing a MODULE in a USE statement. Directories can be added to the search path with the -M command-line option. However, individual .mod files cannot be specified directly on the command line.