Debugging

Contents
Compile for debug
Strategic prints
gdb basics
Halve the code

Compile for debug

A debug build turns on extra checks and turns off optimization so the program behaves in a simple and predictable way.

Compile with -g and -O0 plus -fcheck=all and -Wall -Wextra while hunting a bug. The binary runs slower but reports bounds errors and suspicious code early.

gfortran -g -O0 -fcheck=all -Wall -Wextra main.f90 -o main_dbg

main_dbg built with checks and no optimization

Keep one debug build next to the fast build. Reproduce the failure in the debug binary first, because the error message often points at the exact line.

Strategic prints

Small labeled prints show values at procedure boundaries without flooding the screen.

Print inputs and results with a text label, then remove the lines after the fix. Two clear lines beat twenty unclear ones.

program debug_print implicit none integer :: i real :: x x = 0.0 do i = 1, 5 x = x + real(i) print *, "i =", i, "x =", x end do end program debug_print

gdb basics

The gdb debugger lets you pause a program, inspect variables, and step line by line. Think of it as prints on demand.

The core ideas are breakpoint to mark a stop line, run to start, print to show a value, next to move one line, and quit to leave. Learn these five first.

gdb ./main_dbg

set breakpoint, run, print a variable, step to the next line

Always debug the binary built with -g. Without debug symbols the debugger shows addresses instead of variable names and line numbers.

Halve the code

When nothing is obvious, halve the suspect area. Disable half the logic, rerun, and keep the half that still fails.

Each run removes half the mystery, so even a large file shrinks to one faulty loop in a few steps.

program halve_demo implicit none integer :: vals(4) integer :: k vals = [1, 2, 3, 4] do k = 1, 4 print *, "k =", k, "v =", vals(k) end do end program halve_demo

Next: C interoperability

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

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: