G95: Using the Compiler



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

G95 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 will not be outlined here.

Basic Usage

G95 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, G95 will read from these same file.

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 don't 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, g95 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 vs. 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 doesn't 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, a file named bar.f is handed to the Fortran 77 compiler, 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.

It is important to notice that files named *.f are by default assumed to be written in Fortran 77, and are not handled by G95. In order to compile such files with G95, one thus has to explicitly give -x f95, as in gfortran -c -x f95 bar.f.

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 G95 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 G95 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's nothing guaranteed. In the final link step you should also explicitly specify the libraries of both compilers, i.e. -lgfortran for G95, -lg2c for G77.

Running the Compiler Frontend

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

When given the command line option -fdump-parse-tree, G95 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 oldtimers: ASSIGN in these dumps doesn't refer to the ASSIGN statement, but to the operation of assignment, i.e. sloppily speaking, the = operator.