gfortran: Using the Compiler

Contents

Introduction

This page is meant to give a quick overview on how to use gfortran. It is merely a substitute for a complete manual, which has not yet been written.

Gfortran builds on GCC, and thus shares most characteristics with it. If you know how to use GCC, there will not be much new information for you in this document. Especially, the options for optimization and the generation of debugging information are not outlined here.

Basic Usage

Gfortran is used to compile a source file, source.f90, to an object file, object.o, or an executable, executable. Along the way it generates module description files for the modules it encounters, these are named nameofmodule.mod. If a module is used, gfortran will read from these same files.

In order to compile the source file source.f90, one would run: gfortran -c source.f90
The output file will automatically be named source.o. This is an object file, which cannot be executed.

Once you have compiled some source files, you can link them together with the necessary libraries to generate an executable. This is done as follows: gfortran -o executable object1.o object2.o ..., where the the executable will be named executable and the objectX.o are object files, which may have been created as above, or equally well by another compiler from sources in a different language. If -o executable is omitted, the executable will be named a.out (on cygwin systems: a.exe). The executable may then be executed like any other program.

One may also skip the separate compilation step, and enter a command such as gfortran -o executable source1.f90 source2.f90 which will compile the source files source1.f90 and source2.f90, link and generate the executable executable in one step. You can also put object files on this command line, they will be automatically linked in during the link step.

Advanced Options

Sometimes the basic possibilities given above do not match the user's needs. Therefore this section outlines other stuff the user might want to know.

Compiling Fixed Form Sources

When gfortran is run on a file, whose name ends in .f90 or .f95, gfortran assumes a free form source file. If that file actually is a fixed form source file, the user has to give the -ffixed-form command line option. The precise semantics of this option, and other options relating to fixed form versus free form input are the same as in g77, and may be found in g77's documentation.

Compiling Files Not Named *.f9[05]

When running gfortran one actually does not run the compiler, but the compiler driver. This driver interprets the command line options given, and hands the work off to the actual compiler, the assembler, and the linker. By default, this compiler driver decides by the extensions of the given file names what to do. A file named foo.c is handed to the C compiler and a file named moo.f90 is handed to the Fortran 95 compiler, etc. To overrule this behavior, one has to precede the filename by the argument -x lang, where lang is a string identifying the requested language. For Fortran 95 this is f95.

Since Fortran allows for two different kinds of input, free form source code and fixed form source code, the compiler has to know which kind of input it is given. The rule in place is as follows: files whose name ends in .f or .for are assumed to be fixed form, files whose name end in .f90 or .f95 are assumed to to be free form, for other files the source form has to be explicitly given. This may be done by the command line options described above, which may also be used to override these rules.

Compatibility with g77

In order to efficiently implement the passing of array sections, binary compatibility to Fortran 77 had to be abandoned. If the user wishes to link his sources with old Fortran 77 codes, the command line option -fg77-calls changes back to the old calling convention used by g77.

When linking with code compiled by g77, one also has to take care, because g77 and gfortran use different libraries. Especially I/O might get messed up due to this. Your safest bet is to only use I/O in either the g77 compiled parts or the gfortran compiled parts, but not both, and to use the compiler driver of the part which uses I/O in the final link step. There might be circumstances where doing I/O in both works, but there is nothing guaranteed. In the final link step you should also explicitly specify the libraries of both compilers, i.e. -lgfortran for gfortran, -lg2c for g77.

Running the Compiler Frontend

One may use gfortran as a syntax checker (or verify that gfortran's frontend correctly accepts or rejects a program), by specifying -fsyntax-only on the command line. Gfortran will then not generate object files.

When given the command line option -fdump-parse-tree, gfortran will print a representation of the parsed program, detailing both the data objects and the executable statements of the program in a Lisp-inspired notation. One remark for Fortran old timers: ASSIGN in these dumps does not refer to the ASSIGN statement, but to the operation of assignment, i.e. sloppily speaking, the = operator.