LinuxCommandLibrary

gfortran

Compile Fortran code

TLDR

Compile multiple source files into an executable

$ gfortran [path/to/source1.f90 path/to/source2.f90 ...] -o [path/to/output_executable]
copy

Show common warnings, debug symbols in output, and optimize without affecting debugging
$ gfortran [path/to/source.f90] -Wall -g -Og -o [path/to/output_executable]
copy

Include libraries from a different path
$ gfortran [path/to/source.f90] -o [path/to/output_executable] -I[path/to/mod_and_include] -L[path/to/library] -l[library_name]
copy

Compile source code into Assembler instructions
$ gfortran -S [path/to/source.f90]
copy

Compile source code into an object file without linking
$ gfortran -c [path/to/source.f90]
copy

SYNOPSIS

gfortran [options...] input-files... -o output-file

PARAMETERS

-o file
    Specify output file name

-c
    Compile to object file only, no linking

-g
    Generate debugging information

-Olevel
    Optimization level (0-3, s, fast)

-Wall
    Enable most warning messages

-Werror
    Treat warnings as errors

-std=f95|f2003|f2008|f2018
    Specify Fortran standard conformance

-ffree-form
    Use free-form source format

-ffixed-form
    Use fixed-form source format

-fbounds-check
    Enable array bounds checking

-fcheck=all
    Enable all runtime checks (bounds, pointers, etc.)

-fopenmp
    Enable OpenMP parallel programming support

-J dir
    Directory for module files (.mod)

-I dir
    Add include directory for preprocessing

-L dir
    Add library search directory

-lname
    Link with library libname

-cpp
    Enable C preprocessor

-fPIC
    Generate position-independent code

-shared
    Create shared object file

-v
    Verbose output, show commands

--version
    Print compiler version

-flto
    Enable link-time optimization

-march=cpu
    Generate code for specific CPU

DESCRIPTION

gfortran is the command-line frontend for the GNU Fortran compiler within the GNU Compiler Collection (GCC). It compiles Fortran source code from standards like Fortran 77, 90, 95, 2003, 2008, and partial 2018/2023 into object files or executables.

It supports both fixed-form (e.g., .f, .for) and free-form (e.g., .f90, .f08) source files, with preprocessing via -cpp. Fortran modules are generated as .mod files, placed in directories specified by -J.

gfortran shares most options with gcc, including optimization (-O), debugging (-g), and warnings (-Wall). Fortran-specific flags control bounds checking (-fbounds-check), OpenMP parallelization (-fopenmp), and runtime checks (-fcheck=all). Linking requires the libgfortran runtime library.

Typical usage: gfortran -o program program.f90. It produces position-independent executables by default and supports cross-compilation via target options.

CAVEATS

Full option list is extensive (inherits from gcc); use gfortran --help or man gfortran. Runtime requires libgfortran; bounds checks slow execution.

MODULE HANDLING

Compiled modules create .mod files; use -J dir to avoid build dir clutter.
Search with -I dir.

STANDARDS SUPPORT

Full: F95/2003/2008; Partial: F2018/2023. Extensions via -fdec for DEC compatibility.

PREPROCESSING

-cpp enables #include, #define; pass -Dname for macros.

HISTORY

Development began in 2003 under GCC; first release in GCC 4.0 (2005 preview), stable in GCC 4.1 (2006). Evolved to support modern Fortran standards, now integral to GCC releases.

SEE ALSO

gcc(1), g++(1), gcov(1), ld(1), libgfortran(3)

Copied to clipboard