Sun Studio 12: Fortran User's Guide

2.2 Invoking the Compiler

The syntax of a simple compiler command invoked at a shell prompt is:

f95 [options] files...

Here files… is one or more Fortran source file names ending in .f, .F, .f90, .f95, .F90, .F95, or .for; options is one or more of the compiler option flags. (Files with names ending in a .f90 or .f95 extension are “free-format” Fortran 95 source files recognized only by the f95 compiler.)

In the example below, f95 is used to compile two source files to produce an executable file named growth with runtime debugging enabled:


demo% f95 -g -o growth growth.f fft.f95

Note –

You can invoke the Fortran 95 compiler with either the f95 or f90 command.


New: The compiler will also accept source files with the extension .f03 or .F03. These are treated as equivalent to .f95 and .F95 and could be used as a way to indicate that a source file contains Fortran 2003 extensions.

2.2.2 Command-Line File Name Conventions, describes the various source file extensions accepted by the compiler.

2.2.1 Compile-Link Sequence

In the previous example, the compiler automatically generates the loader object files, growth.o and fft.o, and then invokes the system linker to create the executable program 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 file is written.

2.2.2 Command-Line File Name Conventions

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 Filename Suffixes Recognized by the Fortran 95 Compiler

Suffix  

Language  

Action  

.f

Fortran 77 or Fortran 95 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.

.f95.f90

Fortran 95 free-format 

Same action as .f

.f03

Fortran 2003 free-format 

Same action as .f

.for

Fortran 77 orFortran 95 

Same action as .f.

.F

Fortran 77 or Fortran 95 fixed-format 

Apply the Fortran (or C) preprocessor to the Fortran 77 source file before compilation. 

.F95.F90

Fortran 95 free-format 

Apply the Fortran (or C) preprocessor to the Fortran 95 free-format source file before Fortran compiles it. 

.F03

Fortran 2003 free-format 

Same as .F95

.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,.s.o,.so.n

Libraries 

Pass names of libraries to the linker. .a files are static libraries, .so and .so.n files are dynamic libraries.

Fortran 95 free-format is described in 4.1 Source Language Features.

2.2.3 Source Files

The Fortran compiler 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). Applications may be configured with one source code procedure per file, or by gathering procedures that work together into single files. The Fortran Programming Guide describes the advantages and disadvantages of these configurations.

2.2.4 Source File Preprocessors

f95 supports 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 compiler 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 Fortran-specific source preprocessor. See the fpp(1) man page and the fpp README for details. It is invoked by default on files with a .F, .F90, F95, or .F03 extension.

The source code for fpp is available from the Netlib web site at

http://www.netlib.org/fortran/

See cpp(1) for information on the standard Unix C language preprocessor. Use of fpp over cpp is recommended on Fortran source files.

2.2.5 Separate Compiling and Linking

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% f95 -c file1.f                          (Make new object file)
demo% f95 -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).

2.2.6 Consistent Compiling and Linking

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 some options requires linking with the same options. Also, a number of options require that all source files be compiled with that option, including the link step.

The option descriptions in Chapter 3 identify such options.

Example: Compiling sbr.f with -fast, compiling a C routine, and then linking in a separate step:


demo% f95 -c -fast sbr.f
demo% cc -c -fast simm.c
demo% f95 -fast sbr.o simm.o        link step; passes  -fast  to the linker

2.2.7 Unrecognized Command-Line Arguments

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:

For example:


demo% f95 -bit move.f           <-  -bit is not a recognized f95 option
f95: Warning: Option -bit passed to ld, if ld is invoked, ignored otherwise
demo% f95 fast move.f           <-   The user meant to type -fast
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 f95 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 f95 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!

2.2.8 Fortran 95 Modules

f95 automatically creates module information files for each MODULE declaration encountered in the source files, and searches for modules referenced by a USE statement. For each module encountered (MODULE module_name), the compiler generates a corresponding file, module_name.mod, in the current directory. For example, f95 generates the module information file list.mod for the MODULE list unit found on file mysrc.f95 .

See the -Mpath and -moddir dirlist option flags for information on how to set the defaults paths for writing and searching for module information files.

See also the -use compiler option for implicitly invoking MODULE declarations in all compilation units.

Use the fdumpmod(1) command to display information about the contents of a .mod module information file.

For detailed information, see 4.9 Module Files.