Getting started with gfortran

You have downloaded and installed gfortran (or it was already installed with your system), and now you would like to use it. This page is here to explain the basic use of the compiler and to teach you its most commonly used options. Content is in increasing order of accointance with the compiler.

Getting one file compiled

If your program is only in one file (a hello-world program, or any simple code that doesn't require external libraries), the compilation is straightforward:

gfortran myfile.f

gfortran will then create an executable from your code. This executable will be called a.out on unix systems and a.exe on Windows. Of course, you might consider giving it a more appropriate name: you can specify the name you want with the -o option:

gfortran myfile.f -o program.exe

Getting one file compiled: static linking

_This section does not apply to Windows users, except for Cygwin users with gcc4-4.3.2-2 or later._

gfortran is composed of two main parts: the compiler, which creates the executable program from your code, and the library, which is used when you run your program afterwards. That explains why, if gfortran is installed in a non-standard directory, it may compile your code fine but the executable may fail with an error message like library not found . One way to avoid this (more ideas can be found on the binaries page) is to use the so-called "static linking", available with option -static gfortran then put the library code inside the program created, thus enabling it to run without the library present (like, on a computer where gfortran is not installed). Complete example is:

gfortran -static myfile.f -o program.exe

Free-form, fixed-form and file extensions

By default, gfortran is aware of a few file extensions, and its action depends on the extension of the file you ask it to compile. Examples:

There are other extensions recognized, such as: .f95 is the same as .f90 and .F95 is the same as .F90

Of course, you can override gfortran's default, with options -ffree-form and -ffixed-form (many gfortran options are named -f''=option-name '. For example, if you have a free-form code named bessel.f, you can compile it with

gfortran -ffree-form bessel.f -o bessel.exe

Compiling programs in more than one file

Let's have an example in which the code is split into three different files: aux.f diag.f and flow.f with the main program in flow.f You need first to compile all files other than the main program intoobject files and then compile the main code together with these object files:

gfortran -c aux.f
gfortran -c diag.f
gfortran flow.f aux.o diag.o -o flow.exe

The -c option tells gfortran to create an object file (named aux.o if the code was aux.f , to be linked later with the main program gfortran flow.f aux.o diag.o -o flow.exe .

Compiling programs with external libraries

Standard conformance and other warnings

The standard against which the compiler will check your code is to be specified with the -std option. Possible values are, f95 (Fortran 95), f2003 (Fortran 2003, f2008 (Fortran 2008), gnu (default, Fortran with GNU extensions) and legacy (which enables all possible extensions, even the ugly, old ones that you probably don't want). To compile Fortran 95 code, for example:

gfortran -std=f95 mycode.f

A few more switches should be added if you want gfortran to warn you about all possible standard violations: -Wextra -Wall and -pedantic This is highly recommended, enabling you to check for possible errors in your code, such as syntax errors, unused variables, etc. The following is what you should use in most cases:

gfortran -std=f95 -Wextra -Wall -pedantic mycode.f

Another useful switch is -fbounds-check which causes array indices to be checked. Note that as of this writing, substring operations are not checked.

Compiling a mixed C-Fortran program (main program is Fortran)

Consider this fortran code

  program f_prog
    integer :: i,j(2)
    real :: r
    character(len=10) :: str

    i = 4711
    j = (/13, 14/)
    r = 4712.0
    str = "abcd"
    call c_sub(trim(str)//char(0), i, r, j)
  end program f_prog

and this C code

#include <stdio.h>
  void c_sub_(char *str, int *i, float *r, int j[], int str_len){
    printf("%s %d %d %f %d %d\n", str, str_len, *i, *r, j[0], j[1]);

Notes: gfortran adds a trailing underscore to the identifier, hence the c_sub_. Fortran always passes pointers. Strings cause an additional int argument at the end of the argument list. They must be converted to C conventions, aka trimmed and with an additional char(0) at the end.
Be carefull when you pass defined types; make sure you understand what the -fpack-derived compiler switch does.
Arrays are passed as pointers, too. Do *NOT* provide an interface, otherwise you get an array descriptor which is a complicated thing. Do not get confused about indices, fortran starts at 1, C always at 0.
You compile these examples like:

  gfortran -c fprog.f90
  gcc -c -fno-leading-underscore csub.c
  gfortran fprog.o csub.o -o fprog

Important here is the -fno-leading-underscore, otherwise the linker would see c_subinstead of c_sub_ which we need.

abcd 5 4711 4712.000000 13 14

Compiling a mixed C-Fortran program (main program is C)

None: GFortranGettingStarted (last edited 2013-08-09 12:13:17 by tschwinge)