Build with gfortran and make

Contents
-Wall -Wextra -fcheck
-J module dir
Minimal Makefile
fpm concept

-Wall -Wextra -fcheck

Strict flags turn silent bugs into loud compile time errors. Wall and Wextra enable useful warnings while fcheck adds runtime checks for bounds and more. Keep them on for debug builds from day one.

gfortran -Wall -Wextra -fcheck=all -g app.f90 -o app

./app

n = 42

-J module dir

Modules compile to .mod files that other units import at build time. The -J flag chooses the directory for those files so build roots stay clean. The -I flag tells later compiles where to find them.

mkdir -p build_mods

gfortran -J build_mods -c utils.f90

gfortran -I build_mods main.f90 utils.o -o app

Minimal Makefile

A minimal Makefile rebuilds only stale files with one word: make. List object files as prerequisites of the app target and add a pattern rule for compiling. A clean target removes build artifacts. Recipe lines below start with a tab character, which make requires.

FC = gfortran FFLAGS = -Wall -Wextra -fcheck=all app: main.o utils.o $(FC) $(FFLAGS) main.o utils.o -o app %.o: %.f90 $(FC) $(FFLAGS) -c $< -o $@ clean: rm -f app *.o *.mod .PHONY: clean

make

gfortran -Wall -Wextra -fcheck=all -c main.f90 -o main.o gfortran -Wall -Wextra -fcheck=all -c utils.f90 -o utils.o gfortran -Wall -Wextra -fcheck=all main.o utils.o -o app

fpm concept

The Fortran package manager fpm wraps this workflow in standard commands. Fpm new starts a layout, fpm build compiles, and fpm test runs the suite. Learn the concept here and install the tool when a project outgrows plain makefiles.

fpm new demo

fpm build

fpm test

Next: Debugging

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: