Compilation

Fortran source code is compiled by executing the desired compiler with given input and output files. The following basic example can be built with all modern compilers:

! hello.f90
program main
    print '(a)', 'Hello, World!'
end program main

Save the source code to hello.f90, and execute GNU Fortran:

$ gfortran13 -o hello hello.f90

In most cases, the name of the compiler binary is just gfortran, without the explicit version number.

The binaries compiled with GNU Fortran may depend on the shared runtime library /usr/local/lib/gcc13/libgfortran.so (path may differ), unless the flag -static-libgfortran is used. Make sure to link libgfortran.a statically in order to run the executable on systems without the respective GNU Fortran compiler version installed.

The example program will be accepted by other compilers as well, like Flang:

$ flang -o hello hello.f90

Or, Intel:

$ ifort -o hello hello.f90

All compilers output a binary hello, we can execute:

$ ./hello
Hello, World!

Compiler Flags

The behaviour of a compiler can be controlled by command-line flags. The table describes some of the flags of GNU Fortran.

Parameter Description
-Wall Enable all warnings.
-Werror Turn all warnings into errors.
-c Compile source code without linking.
-g Generate debug information to be used by a debugger (GDB).
-o file Set file name of output to file.
-I path Add path to the include search path.
-J path Specifies where to put Fortran .mod files for compiled modules.
-L path Add path to the library search path.
-On Level of optimisation to use, where n = 0, 1, 2, 3.
-Os Optimise for binary size.
-Wl,-rpath=path Set runtime library search path to path, for example, -Wl,-rpath=/usr/local/bin/gcc13/. May be required for older versions of GNU Fortran on FreeBSD.
-std=std Set Fortran language standard to std, which may be one of f95, f2003, f2008, f2018, gnu, or legacy. The default value is gnu.
-fbackslash Change the interpretation of backslashes in string literals from a single backslash character to C-style escape characters.
-ffree-form Force free-format. Required for Fortran ≥ 90 source code files with ending .f.
-ffree-line-length-n Set column after which characters are ignored in typical free-form lines in the source file. The default value is 132. n may be none, meaning that the entire line is meaningful. -ffree-line-length-0 is the same as -ffree-line-length-none. Use if lines have more than 132 characters.
-fimplicit-none Specify that no implicit typing is allowed, unless overridden by explicit implicit statements. This is the equivalent of adding implicit none to the start of every program or module.
-fmax-errors=n Limit the maximum number of error messages to n. If n is set to 1, compilation aborts after the occurrence of the first error.
-static-libgfortran Compile libgfortran statically into the executable. Otherwise, the dynamically linked library must be present in the library search path in order to run the program.

On FreeBSD, it is often necessary to set the include and library search paths manually, if a program is linked against a system-wide installed library, for instance:

$ gfortran13 -I/usr/local/include -L/usr/local/lib -o example example.f90 -lX11