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. To compile the example with GNU Fortran, run:

$ gfortran10 -o hello hello.f90

On FreeBSD, it may be required to set the GNU Fortran runtime library search path with parameter -Wl,-rpath=/usr/local/lib/gcc10/ manually. The path depends on the installed version of GCC. In most cases, the argument can be omitted.

Binaries compiled with GNU Fortran may depend on the shared runtime library /usr/local/lib/gcc10/, unless the flag -static-libgfortran is used. Make sure to compile libgfortran.a statically into the binary in order to run the executable on systems without the used GNU Fortran compiler version installed.

The example can be compiled with any other compiler, like Flang:

$ flang -o hello hello.f90

The 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 shows some of the flags accepted by GNU Fortran.

Flag 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.
-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. May be required 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.

XWindow Programming Environment

XWindow Programming Environment (xwpe)
Fig. 1: Project options in xwpe

The XWindow Programming Environment (xwpe) IDE allows to compile and run a Fortran project from the top menu. Open the “Compiler Options” window under OptionsCompilerFortran. On FreeBSD, set:

Option Value
Compiler gfortran10
Compiler-Options -Wl,-rpath=/usr/local/lib/gcc10/
File-Postfix .f90
Compiler-Style GNU

Then, under ProjectOptions, set additional compiler options, the name of the executable, and further arguments. Select RunCompile in the menu to compile the program.