Editing, compiling, and linking programs with XL Fortran

The compiler phases
Editing Fortran source files
Compiling with XL Fortran
Compiling Fortran 95 or Fortran 90 programs
Compiling parallelized XL Fortran applications
POSIX Pthreads API support
XL Fortran input files
XL Fortran output files
Specifying compiler options
Linking XL Fortran programs
Compiling and linking in separate steps
Dynamic and static linking

Basic Fortran program development consists of repeating cycles of editing, compiling and linking (by default a single step combined with compiling), and running.

Prerequisite Information:
  1. Before you can use the compiler, you must first ensure that all Linux settings (for example, certain environment variables and storage limits) are correctly configured. For more information see Environment variables and XL Fortran.
  2. To learn more about writing Fortran programs, refer to the XL Fortran Advanced Edition V10.1 for Linux Language Reference.

The compiler phases

The typical compiler invocation command executes some or all of the following programs in sequence. For link time optimizations, some of the phases will be executed more than once during a compilation. As each program runs, the results are sent to the next step in the sequence.

  1. A preprocessor
  2. The compiler, which consists of the following phases:
    1. Front-end parsing and semantic analysis
    2. Loop transformations
    3. Interprocedural analysis
    4. Optimization
    5. Register allocation
    6. Final assembly
  3. The assembler (for .s files and for .S files after they are preprocessed)
  4. The linker ld

To see the compiler step through these phases, specify the -qphsinfo and -v compiler options when you compile your application.

Editing Fortran source files

To create Fortran source programs, you can use any of the available text editors, such as vi or emacs. Source programs must use a recognized filename suffix unless the configuration file defines additional non-standard filename suffixes. See XL Fortran input files for a list of filename suffixes recognized by XL Fortran.

For a Fortran source program to be a valid program, it must conform to the language definitions specified in the XL Fortran Advanced Edition V10.1 for Linux Language Reference.

Compiling with XL Fortran

To compile a source program, use one of the compiler invocation commands with the syntax shown below:

Read syntax diagramSkip visual syntax diagram>>-compiler_invocation------------------------------------------>
 
   .-----------------------------------------.
   | .---------------------.                 |
   V V                     |                 |
>------+-----------------+-+----input_file---+-----------------><
       '-cmd_line_option-'
 

The compiler invocation command performs all necessary steps to compile Fortran source files, assemble any .s and .S files , and link the object files and libraries into an executable program.

When working with source files whose filename extensions indicates a specific level of Fortran, such as .f95, .f90, or f77, compiling with the xlf or threadsafe counterpart invocations will cause the compiler to automatically select the appropriate language-level defaults. The other base compiler invocation commands exist primarily to provide explicit compilation support for different levels and extensions of the Fortran language.

In addition to the base compiler invocation commands, XL Fortran also provides specialized variants of many base compiler invocations. A variation on a base compiler invocation is named by attaching a suffix to the name of that invocation command. Suffix meanings for invocation variants are:

_r
Threadsafe invocation variant that supports POSIX Pthread APIs for multithreaded applications, including applications compiled with -qsmp or with source code containing OpenMP program parallelization directives.
Table 4. XL Fortran compiler invocation commands
Base Invocation Available Invocation Variants Description
xlf
f77
fort77
xlf_r Invokes the compiler so that source files are compiled as FORTRAN 77 source code.
xlf90
f90
xlf90_r Invokes the compiler so that source files are compiled as Fortran 90 source code.
xlf95
f95
xlf95_r Invokes the compiler so that source files are compiled as Fortran 95 source code.

Compiling Fortran 95 or Fortran 90 programs

Use the following invocations (or their variants) to conform more closely to their corresponding Fortran language standards:

f95, xlf95
Fortran 95
f90, xlf90
Fortran 90

These are the preferred compiler invocation commands that you should use when creating and compiling new applications.

They all accept Fortran 90 free source form by default. To use fixed source form with these invocations, you must specify the -qfixed command line option.

I/O formats are slightly different between these commands and the other commands. I/O formats for the xlf95 invocation are also different from those of xlf90. We recommend that you switch to the Fortran 95 formats for data files whenever possible.

By default, those invocation commands do not conform completely to their corresponding Fortran language standards. If you need full compliance, compile with the following additional compiler option settings:

-qnodirective -qnoescape -qextname -qfloat=nomaf:nofold
 -qnoswapomp -qlanglvl=90std -qlanglvl=95std

Also, specify the following runtime options before running the program, with a command similar to the following:

export XLFRTEOPTS="err_recovery=no:langlvl=90std"

The default settings are intended to provide the best combination of performance and usability, so you should change them only when absolutely required. Some of the options mentioned above are only required for compliance in very specific situations. For example, you would need to specify -qextname when an external symbol, such as a common block or subprogram, is named main.

Compiling parallelized XL Fortran applications

XL Fortran provides threadsafe compilation invocations that you can use when compiling parallelized applications for use in multiprocessor environments.

These invocations are similar to their corresponding base compiler invocations, except that they link and bind compiled objects to threadsafe components and libraries.

Note:
Using any of these commands alone does not imply parallelization. For the compiler to recognize OpenMP directives and activate parallelization, you must also specify -qsmp compiler option. In turn, you can only specify the -qsmp option in conjunction with one of these six invocation commands. When you specify -qsmp, the driver links in the libraries specified on the smp libraries line in the active stanza of the configuration file.

POSIX Pthreads API support

XL Fortran supports thread programming with the IEEE 1003.1-2001 (POSIX) standard Pthreads API.

XL Fortran input files

The input files to the compiler are:

Source files (.f .F ,f77 .F77 .f95 .f90 .F90 .F95 suffixes)
The compiler considers files with these suffixes as being Fortran source files for compilation.

The compiler compiles source files in the order you specify on the command line. If it cannot find a specified source file, the compiler produces an error message and proceeds to the next file, if one exists.

Files with a suffix of .F, F77, F95, or F90 are passed through the C preprocessor (cpp) before being compiled.

Include files also contain source and often have suffixes different from those ordinarily used for Fortran source files.

Object files (.o suffix)
After the compiler compiles the source files, it uses the ld command to link the resulting .o files, any .o files that you specify as input files, and some of the .o and .a files in the product and system library directories. The compiler can then produce a single .o object file or a single executable output file from these object files.
Assembler source files (.s and .S suffixes)
The compiler sends assembler source files to the assembler (as). The assembler sends object files to the linker at link time.
Note:
Assembler source files with a .S filename suffix are first preprocessed by the compiler, then sent to the assembler.
Shared object or library files (.so suffix)
These are object files that can be loaded and shared by multiple processes at run time. When a shared object is specified during linking, information about the object is recorded in the output file, but no code from the shared object is actually included in the output file.
Configuration files (.cfg suffix)
The contents of the configuration file determine many aspects of the compilation process, most commonly the default options for the compiler. You can use it to centralize different sets of default compiler options or to keep multiple levels of the XL Fortran compiler present on a system.

The default configuration file is /etc/opt/ibmcmp/xlf/10.1/xlf.cfg.

Module symbol files: modulename.mod
A module symbol file is an output file from compiling a module and is an input file for subsequent compilations of files that USE that module. One .mod file is produced for each module, so compiling a single source file may produce multiple .mod files.
Profile data files

The -qpdf1 option produces runtime profile information for use in subsequent compilations. This information is stored in one or more hidden files with names that match the pattern .*pdf*.

XL Fortran output files

The output files that Fortran produces are:

Executable files: a.out
By default, XL Fortran produces an executable file that is named a.out in the current directory.
Object files: filename.o
If you specify the -c compiler option, instead of producing an executable file, the compiler produces an object file for each specified program source input file, and the assembler produces an object file for each specified assembler input file. By default, the output object files are saved to the current directory using the same file name prefixes as their corresponding source input files.
Assembler source files: filename.s
If you specify the -S compiler option, instead of producing an executable file, the XL Fortran compiler produces an equivalent assembler source file for each specified input source file. By default, the output assembler source files are saved to the current directory using the same file name prefixes as their corresponding source input files.
Compiler listing files: filename.lst
By default, no listing is produced unless you specify one or more listing-related compiler options. The listing file is placed in the current directory, with the same file name prefix as the source file.
Module symbol files: modulename.mod
Each module has an associated symbol file that holds information needed by program units, subprograms, and interface bodies that USE that module. By default, these symbol files must exist in the current directory. Compiling modules will also produce .o files that are needed when linking if you use the module.
Preprocessed source files: Ffilename.f
If you specify the -d option when compiling a file with a .F suffix, the intermediate file created by the C preprocessor (cpp) is saved rather than deleted.
Profile data files (.*pdf*)
These are the profile-directed feedback files that the -qpdf1 compiler option produces. They are used in subsequent compilations to tune optimizations according to actual execution results.

Specifying compiler options

Compiler options perform a variety of functions, such as setting compiler characteristics, describing the object code to be produced, controlling the diagnostic messages emitted, and performing some preprocessor functions.

You can specify compiler options:

When multiple compiler options have been specified, it is possible for option conflicts and incompatibilities to occur. To resolve these conflicts in a consistent fashion, the compiler usually applies the following general priority sequence:

  1. Directive statements in your source file override command line settings
  2. Command line compiler option settings override configuration file settings
  3. Configuration file settings override default settings

Generally, if the same compiler option is specified more than once on a command line when invoking the compiler, the last option specified prevails.

Note:
The -I compiler option is a special case. The compiler searches any directories specified with -I in the xlf.cfg file before it searches the directories specified with -I on the command line. The option is cumulative rather than preemptive.

Other options with cumulative behavior are -R and (lowercase L).

You can also pass compiler options to the linker, assembler, and preprocessor. See XL Fortran Compiler-option reference for more information about compiler options and how to specify them.

Linking XL Fortran programs

By default, you do not need to do anything special to link an XL Fortran program. The compiler invocation commands automatically call the linker to produce an executable output file. For example, running the following command:

xlf95 file1.f file2.o file3.f

compiles and produces the object files file1.o and file3.o, then all object files (including file2.o) are submitted to the linker to produce one executable.

After linking, follow the instructions in Running XL Fortran programs to execute the program.

Compiling and linking in separate steps

To produce object files that can be linked later, use the -c option.

xlf95 -c file1.f                # Produce one object file (file1.o)
xlf95 -c file2.f file3.f        # Or multiple object files (file2.o, file3.o)
xlf95 file1.o file2.o file3.o   # Link object files with appropriate libraries

It is often best to execute the linker through the compiler invocation command, because it passes some extra ld options and library names to the linker automatically.

Dynamic and static linking

XL Fortran allows your programs to take advantage of the operating system facilities for both dynamic and static linking:

See Linking XL Fortran programs for more information about linking your programs.