Invocation directe du compilateur

Nom du fichier source

Let us consider the basic case in Fortran where we have only one file to compile.

Advice: do not use space, diacritical marks or any special character in the name of the file.

Quel suffixe choisir pour le nom du fichier ? Pour répondre à cette question, il faut connaître la distinction entre "format fixe" et "format libre" d'un fichier source Fortran.

Aside, free versus fixed source form:

  • Fixed source form: this was how Fortran programs were written in the old times (in Fortran 77 and before). Statements had to be written between columns 7 and 72. (There were also other formatting rules.)

  • Free source form: appeared in Fortran 90. That is what we use now.

  • We have to tell the compiler whether the file is supposed to be in fixed of free source form, either through the suffix of the filename or through a compilation option.

La plupart des compilateurs considèrent par défaut que le suffixe .f signifie format fixe et .f90 format libre.

Manipulation 1

  • Écrire un programme trivial (du type hello).
  • Le compiler. Il faut connaître la commande qui invoque le compilateur : c'est gfortran pour le compilateur GNU, ifort pour celui d'Intel, nagfor pour celui de NAG…
  • L'exécuter.

Importance of compilation options

La simple invocation du compilateur, par exemple par la commande :

gfortran plouf.f90

est insuffisante pour un programme non trivial, même contenu dans un seul fichier. Deux raisons :

  • Importance des options de débogage ou des options d'optimisation.
  • Options pour l'utilisation de bibliothèques1.

Do not miss the best of the compiler! You depend on the compiler for:

  • debugging your program

  • fast execution of your program

However you cannot have both those services in a single compilation. Debugging a code and producing an efficient executable file are opposite requirements for the compiler. So you have to choose what you ask to the compiler.

Two situations:

  • When you are developing your program: test it on small cases → use debuging options of the compiler.

  • When you are satisfied with your code and want to produce results for really useful cases → use compilation options producing a fast executable.

Remarque : Fortran est devenu le langage des gros projets. Les options de débogage ou d'optimisation seront donc importantes pour tous les programmes en Fortran que vous écrirez ou utiliserez, et vous ferez très probablement appel à des bibliothèques.

Options for debugging

  • To produce warnings for valid but suspicious statements: maybe not what you intended. For example: type conversion, truncation of a string, underflow in floating point computation, statement that can never be reached.

  • To warn of "bad" programming style: using an obsolete feature of the language, using a language extension (this extension may not work with another compiler).

  • To detect errors: invalid floating point operation (division by zero, square root of a negative number, etc.), referring to an out-of-bounds subscript of an array, using in an expression a variable which has not yet been defined.

There are many debugging options:

  • Example for GFortran 11:
-std=f2003 -fmax-errors=1 -pedantic -Wall -Wcharacter-truncation -Wunused-parameter -Wno-conversion -Wimplicit-interface -Wimplicit-procedure -Wno-integer-division -Wintrinsics-std -Wno-maybe-uninitialized -fbacktrace -ffpe-trap=invalid,zero,overflow -g3 -fcheck=bits,bounds,do,mem,pointer,recursion -finit-derived -finit-real=snan -Og
  • man gfortran to see the list of compilation options (debugging options and others) with their descriptions.

Unfortunately, debugging options depend on the compiler:

  • Roughly the same functionality for debugging in all compilers but the names of the options (what you actually type on the command line) change with the compiler.

  • Moreover, for a given compiler, options change a little with the version of the compiler.

  • Advice: keep a list of debugging options for each major version of each compiler that you use. (A major version is usually released once per year or less frequently.)


  1. Cf. définition et intérêt d'une bibliothèque dans le cours sur les bibliothèques