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:

$ gfortran8 -Wl,-rpath=/usr/local/lib/gcc8/ -o hello hello.f90

On FreeBSD, GNU Fortran 8 requires the runtime library search path to be set to /usr/local/lib/gcc8/ manually. The path depends on the installed version of GCC. The -Wl,-rpath argument may be optional on other platforms. All compiled binaries rely on the shared runtime library libgfortran, unless the flag -static-libgfortran is used.

To compile the example with Flang, run:

$ flang -o hello hello.f90

Both compilers create the binary hello:

$ ./hello
Hello, World!

Compiler Flags

The behaviour of compilers 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.
-Wl,-rpath=path Set runtime library search path to path. 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 with file 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 procedure.
-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 installed 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 gfortran8
Compiler-Options -Wl,-rpath=/usr/local/lib/gcc8/
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.