Appendix C: gfortran Setup and Compiler Flags

Everything you need to install the toolchain and a reference to the compiler flags used throughout the book. The book's baseline is gfortran (GNU Fortran) version 10 or later, targeting the Fortran 2018 standard. gfortran is free, open, and available on every major platform, which is why a free textbook can rely on it.

Installing gfortran

Linux

gfortran ships in every distribution's package manager:

# Debian / Ubuntu
$ sudo apt update && sudo apt install gfortran

# Fedora / RHEL
$ sudo dnf install gcc-gfortran

# Arch
$ sudo pacman -S gcc-fortran

For the parallel chapters you will also want an MPI implementation and (for coarrays) OpenCoarrays:

$ sudo apt install libopenmpi-dev openmpi-bin   # MPI (Chapter 34)
$ sudo apt install libcoarrays-dev              # coarrays via OpenCoarrays (Chapter 32)

macOS

Use Homebrew (brew.sh):

$ brew install gcc            # provides gfortran
$ brew install open-mpi       # MPI (Chapter 34)
$ brew install opencoarrays   # coarrays (Chapter 32)

The Homebrew gcc installs gfortran (sometimes as gfortran-14 etc.; a plain gfortran symlink is usually created — check with which gfortran).

Windows

Two good options:

  1. MSYS2 (msys2.org) — a native toolchain. After installing MSYS2: console $ pacman -S mingw-w64-ucrt-x86_64-gcc-fortran Then use the "MSYS2 UCRT64" shell, where gfortran is on the path.
  2. WSL2 (Windows Subsystem for Linux) — install a Linux distribution from the Microsoft Store and follow the Linux instructions above. This is the smoothest route for the parallel chapters, since MPI and OpenCoarrays are packaged for Linux.

Verify

$ gfortran --version
GNU Fortran (…) 13.2.0

Any version 10 or newer is fine for the whole book. (Coarray teams and a few 2018/2023 features want a more recent version; the relevant chapters note this where it matters.)

Compiling: the one command to remember

$ gfortran -std=f2018 -Wall -O2 myprogram.f90 -o myprogram && ./myprogram

If your program is split across modules, list the sources in dependency order (a module before the code that uses it), or let a build tool (fpm, make, CMake) work out the order — see Chapter 16.

Flag reference

Standards and warnings (use always)

Flag Effect
-std=f2018 Enforce the Fortran 2018 standard; warn on extensions. (-std=f2008, -std=f2023 also exist.)
-Wall Enable the common, useful warnings.
-Wextra Additional warnings (e.g., unused variables).
-pedantic Warn on non-standard usage.

Development / debugging (use while writing; see Chapter 13)

Flag Effect
-g Emit debug information (for gdb).
-O0 No optimization — fastest compiles, easiest debugging.
-fcheck=all Run-time checks: array bounds, allocation, pointers. Invaluable while developing.
-fbacktrace Print a stack trace on a crash.
-ffpe-trap=invalid,zero,overflow Trap floating-point exceptions instead of silently producing NaN/Inf.
-finit-real=snan Initialize reals to signaling NaN to catch use-before-set.

Optimization (use for production; see Chapters 29–30)

Flag Effect
-O2 Solid optimization; a safe default.
-O3 More aggressive (loop transforms, vectorization); verify results still agree.
-Ofast -O3 plus -ffast-mathrelaxes IEEE arithmetic; can change results. Use knowingly.
-march=native Tune for this machine's CPU (enables its SIMD instruction sets). Not portable across CPUs.
-flto Link-time optimization — cross-file inlining and analysis.
-funroll-loops Unroll loops (usually implied at -O3).
-fopt-info / -fopt-info-vec Report what the optimizer did (e.g., which loops vectorized).

A rule from Chapter 30: build with -fcheck=all -g -O0 while developing, and switch to -O2 (or -O3 -march=native -flto) for production runs — and remove -fcheck=all from production, since the run-time checks cost speed. Always record the flags you used with your results.

Parallel builds

Flag / driver For Chapter
-fopenmp OpenMP (shared-memory threads) 33
-fcoarray=single Coarrays, one image (for testing serially) 32
-fcoarray=lib + OpenCoarrays caf / cafrun Coarrays, multiple images 32
mpif90 (wrapper around gfortran) + mpirun -np N MPI (distributed processes) 34

Example parallel compiles:

$ gfortran -std=f2018 -O2 -fopenmp heat_omp.f90 -o heat_omp       # OpenMP
$ caf -O2 heat_ca.f90 -o heat_ca && cafrun -n 4 ./heat_ca         # coarrays, 4 images
$ mpif90 -O2 heat_mpi.f90 -o heat_mpi && mpirun -np 4 ./heat_mpi  # MPI, 4 processes

Linking libraries

Flag Links
-llapack -lblas LAPACK and reference BLAS (Chapter 21)
-lopenblas OpenBLAS (a fast BLAS/LAPACK)
-lfftw3 FFTW
`nf-config --flibs` NetCDF-Fortran (use the config tool for the exact flags)

Other compilers (Chapter 30)

The book uses gfortran, but the same code should compile with: - Intel ifx/ifort — often fastest on Intel hardware; flags like -O3 -xHost -ipo -qopt-report. - NVIDIA nvfortran — targets NVIDIA GPUs (OpenACC, CUDA Fortran; Chapter 35). - LLVM flang and LFortran — newer open compilers; LFortran is also interactive.

Portable code compiles cleanly on more than one — a good habit is to build with two compilers in CI (Chapter 37).