gfortran
Compile Fortran code
TLDR
Compile multiple source files into an executable
Show common warnings, debug symbols in output, and optimize without affecting debugging
Include libraries from a different path
Compile source code into Assembler instructions
Compile source code into an object file without linking
SYNOPSIS
gfortran [options] file...
Example: gfortran -Wall -O3 myprogram.f90 -o myexecutable
PARAMETERS
-o file
Specifies the output file name. By default, an executable is named a.out.
-c
Compile or assemble the source files, but do not link. Output is an object file (.o).
-S
Compile only; do not assemble or link. Output is an assembly file (.s).
-E
Preprocess only; do not compile, assemble or link. Output is to standard output.
-g
Generate debugging information suitable for GDB.
-Olevel
Set the optimization level. Common levels are -O1, -O2, -O3 (more aggressive), -Os (optimize for size), and -Og (optimize for debugging).
-Wall
Enable all common warnings.
-Wextra
Enable extra warnings not enabled by -Wall.
-Idir
Add dir to the list of directories to search for include files.
-Ldir
Add dir to the list of directories to search for libraries.
-llib
Link with library lib. E.g., -lm links with libm.so.
-std=standard
Specify the Fortran standard to which the code should conform (e.g., f95, f2003, f2008).
-fPIC
Generate position-independent code, often required for shared libraries.
-fopenmp
Enable OpenMP support for parallel programming.
DESCRIPTION
gfortran is the GNU Fortran compiler, a crucial component of the GNU Compiler Collection (GCC). It serves as the primary tool for translating Fortran source code into executable programs, object files, or libraries on Linux systems. gfortran supports a wide range of Fortran standards, including Fortran 77, Fortran 90, Fortran 95, Fortran 2003, Fortran 2008, and Fortran 2018, ensuring compatibility with both legacy and modern Fortran applications. It is widely used in scientific computing, engineering, and numerical analysis due to Fortran's strengths in these domains. Key features include extensive optimization capabilities for performance tuning, support for various debugging levels, and the ability to link with libraries written in other languages like C or C++. The compiler integrates seamlessly with other GCC compilers (like gcc for C and g++ for C++), allowing for mixed-language programming. gfortran provides a robust and free-software alternative for Fortran development, making it an indispensable tool for Fortran programmers.
CAVEATS
gfortran is a powerful compiler, but users should be aware of a few points:
- Fortran Specifics: Understanding Fortran's strict syntax and data handling is crucial.
- Toolchain Dependencies: It relies on the GCC toolchain (assembler, linker) for its operation.
- Error Messages: While improved, some complex compilation errors can still be challenging to interpret.
- Linking Issues: Incorrect library paths or missing libraries (-L, -l options) are common sources of errors during the linking phase.
- Performance: Achieving optimal performance requires careful use of optimization flags and understanding of Fortran array operations and memory layout.
EXIT STATUS
gfortran typically exits with a status of 0 if compilation and linking are successful. A non-zero exit status (usually 1) indicates an error during the compilation, assembly, or linking process.
ENVIRONMENT VARIABLES
gfortran respects several environment variables inherited from the GCC toolchain, such as CPATH (for include paths), LIBRARY_PATH (for library search paths), and LD_LIBRARY_PATH (for runtime library search paths when executing). These variables can influence how the compiler finds headers and libraries.
HISTORY
gfortran is part of the GNU Compiler Collection (GCC), an ambitious free software project initiated by Richard Stallman. Its development as a full-fledged Fortran 90/95/2003/2008/2018 compiler marked a significant evolution from its predecessor, g77 (the Fortran 77 compiler). While g77 provided a robust Fortran 77 implementation, gfortran was specifically designed to meet the demands of modern Fortran standards, including object-oriented features, module support, and enhanced array handling. It effectively replaced g77 as the default Fortran compiler within the GCC suite and has undergone continuous development, contributing to its stability, performance, and adherence to evolving Fortran standards.