Chapter 2 — Key Takeaways (Setting Up)
A one-page reference for the toolchain. Keep the two build commands and implicit none where you can see
them; they are the muscle memory the rest of the book assumes.
Install gfortran (want version 10+)
| Platform | Command |
|---|---|
| Debian / Ubuntu | sudo apt install gfortran |
| Fedora / RHEL | sudo dnf install gcc-gfortran |
| Arch | sudo pacman -S gcc-fortran |
| macOS (Homebrew) | brew install gcc (gfortran is inside the gcc formula) |
| Windows | MSYS2 (pacman -S mingw-w64-ucrt-x86_64-gcc-fortran) or WSL (then use the Linux command) |
| HPC cluster | module load gcc (do not install; load) |
Verify: gfortran --version → read the version number off the first line. Full detail in Appendix C.
The skeleton of every program
program name
implicit none ! always — no exceptions
! ... declarations, then statements ...
print '(a)', "text" ! flush-left; print * adds a leading blank
end program name ! the name must match `program name`
- Files are
.f90(free-form). Keywords lowercase. Indent two spaces (for humans; the compiler ignores it).
The compile–link–run cycle
source ──compile──▶ object ──link──▶ executable ──run──▶ output
hello.f90 hello.o hello (.exe) ./hello
| Term | What it is |
|---|---|
object file (.o) |
your one source compiled to machine code, references not yet resolved; gfortran -c stops here |
| linker | combines objects + libraries into a runnable program, resolving every reference |
| executable | the finished, runnable program (hello on Linux/macOS, hello.exe on Windows) |
Read the error's stage: has no IMPLICIT type / syntax error = compile. undefined reference to …
= link. Different stage, different fix.
The five flags that matter early
| Flag | What it does |
|---|---|
-std=f2018 |
enforce the 2018 ISO standard; warn on extensions |
-Wall |
enable a broad set of warnings (free code review) |
-O2 |
optimize the generated code for speed (release) |
-g |
embed debug info (line numbers, names) for a debugger |
-fcheck=all |
insert run-time checks (array bounds, etc.) — development |
Two profiles (memorize):
$ gfortran -std=f2018 -Wall -g -fcheck=all file.f90 -o file # develop
$ gfortran -std=f2018 -Wall -O2 file.f90 -o file # release
Rule: develop with checks on; run with optimization on. Never quote a timing measured with -fcheck=all.
(Deeper flags — -O3 -march=native -flto — are Chapter 30.)
Free-form vs fixed-form
Free-form (.f90, what we write) |
Fixed-form (.f/.for, legacy → Ch. 17) |
|
|---|---|---|
| Columns | any column, up to 132 chars | columns 1–6 reserved, code in 7–72 |
| Comment | ! anywhere |
C/* in column 1 |
| Continue a line | trailing & |
a character in column 6 |
You cannot paste fixed-form into a .f90 file unchanged — convert it (Part IV).
implicit none — the non-negotiable habit
- Goes immediately after the
program/module/subroutine/functionline, before declarations. - Turns off implicit typing (undeclared names typed by first letter:
i–n→ integer, else real). - Why it is mandatory: without it, a mistyped variable name is silently invented as a new, uninitialized variable → a wrong answer with no error. With it, that typo is a compile-time error.
The one rule of this chapter:
implicit none, in every program unit, always.
Numbers, the house style (previewed; full story Ch. 3)
use, intrinsic :: iso_fortran_env, only: dp => real64
real(dp) :: x
x = 1.5_dp ! declare reals as real(dp); suffix literals with _dp
print '(a, f0.2)', 'x = ', x→ text plus a real with 2 decimals, minimum width, no leading blank.
Common pitfalls
- Confusing "it compiled" with "it is correct" — the compiler checks grammar, not intent.
- Forgetting
implicit none(silent typo bugs) or-Wall/-fcheck=all(declining free help). - Pasting fixed-form code into a
.f90file. - Timing a
-fcheck=allbuild and calling Fortran slow. - Forgetting the
./to run a program in the current directory on Linux/macOS.
Project piece added this chapter
The driver: program heat — a banner that compiles cleanly, in heat-solver/heat.f90. Built with the
development profile; verified by hand (no arithmetic, so the banner prints verbatim). Every future checkpoint
grows this file or links new files to it, all the way to the Chapter 38 capstone.