ZetCode

Compiling and debugging Pascal

last modified November 19, 2026 Free Pascal 3.2.2

Free Pascal is a command-line compiler, and its flags control everything from optimization to the runtime checks that catch a bad index or an overflowing integer. A compile is a sequence of files: sources become object files, units become .ppu files, and the linker produces the executable. This article builds a program from a unit with a Makefile, inspects a compiled unit, turns on range, overflow, assertion, and heap checks, and follows a crash in gdb.

A unit, a program, and a Makefile

The example has three source files. greeter.pas is a unit with an interface and an implementation; usegreeter.pas is the program that uses it; and Makefile records the build command. The unit's interface declares the function, and its implementation defines the body.

greeter.pas
unit greeter;
{$mode objfpc}{$H+}

interface

function Greeting(const Name: string): string;

implementation

function Greeting(const Name: string): string;
begin
  Result := 'Hello, ' + Name + '!';
end;

end.

The program names the unit in its uses clause and calls the function as if it were local. The compiler finds greeter.pas in the current directory, compiles it first, and links the resulting object into the executable.

usegreeter.pas
program usegreeter;
{$mode objfpc}{$H+}

uses
  greeter;

begin
  WriteLn(Greeting('Pascal'));
end.

The Makefile gives the target, its prerequisites, and the command. The leading tab before $(FPC) is required by make; spaces produce a "missing separator" error. clean removes the executable and the compiled unit files, which is useful when a stale .ppu no longer matches its source.

Makefile
FPC = fpc

all: usegreeter

usegreeter: usegreeter.pas greeter.pas
	$(FPC) -gl usegreeter.pas

clean:
	rm -f usegreeter greeter.ppu greeter.o

fpc compiles the unit automatically when the program depends on it, so there is no separate rule for greeter.ppu. The build produces greeter.ppu, greeter.o, and the executable. The .ppu file holds the interface and is version-specific: it must be rebuilt when the compiler version or the unit source changes.

$ make
fpc -gl usegreeter.pas
Free Pascal Compiler version 3.2.2+dfsg-9ubuntu1 [2022/04/11] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling usegreeter.pas
Linking usegreeter
17 lines compiled, 0.0 sec
$ ./usegreeter
Hello, Pascal!

Inspecting a compiled unit

ppudump reads a .ppu file and prints its header, the units it depends on, and the symbols in its interface. It is the Pascal equivalent of inspecting a C header and object file together, and it answers questions such as "which compiler wrote this unit" and "what exactly does it export".

The header is followed by the interface section, a list of used units, and the symbol table. The dump is long and contains checksums, file positions, and internal definition numbers; the first screen is usually enough to confirm that the unit was built for the right target. A checksum mismatch at compile time means the .ppu belongs to another source or compiler version.

$ ppudump greeter.ppu
PPU-Analyser Version 3.2.2
Copyright (c) 1998-2013 by the Free Pascal Development Team

Analyzing greeter.ppu (v207)

Header
-------
Compiler version        : 3.2.2
Target processor        : x86_64
Target operating system : Linux-x86-64

Range checking

By default an array index is not checked, and a write past the end corrupts whatever follows the array in memory. The -Cr flag, or {$R+} in the source, inserts a bounds check before every index access. The program below deliberately writes one element past a three-element array.

range.pas
program range;
{$mode objfpc}{$H+}

var
  A: array[0..2] of Integer;
  I: Integer;

begin
  for I := 0 to 3 do
  begin
    WriteLn('index ', I);
    A[I] := I;
  end;
end.

The first three iterations succeed and the fourth stops with runtime error 201. The two lines after the error show the failing source line and the call chain; the addresses depend on the build and are different on another machine. The process exit status is 201, which a shell or a test runner can check.

$ fpc -gl -Cr range.pas
$ ./range
index 0
index 1
index 2
index 3
Runtime error 201 at $0000000000401124
  $0000000000401124  main,  line 10 of range.pas
  $0000000000422F2C

Overflow checking

-Co (or {$Q+}) checks arithmetic against the range of the result type. The next program adds one to the largest Int64. Without the flag the value wraps around to the smallest negative number; with the flag the runtime stops the program.

overflow.pas
program overflow;
{$mode objfpc}{$H+}

var
  Big, Sum: Int64;

begin
  Big := 9223372036854775807;
  WriteLn('Big: ', Big);
  Sum := Big + 1;
  WriteLn('Sum: ', Sum);
end.

Runtime error 215 is an integer overflow. Without SysUtils the error is printed in the plain runtime format; with SysUtils in the uses clause the same condition arrives as an EIntOverflow exception and the exit status becomes 217. The flag does not catch a 32-bit Integer overflow in an intermediate expression evaluated as Int64; the assignment check does catch it when the value is stored.

$ fpc -gl -Co overflow.pas
$ ./overflow
Big: 9223372036854775807
Runtime error 215 at $0000000000401118
  $0000000000401118  main,  line 8 of overflow.pas
  $0000000000422F4C

Assertions

Assert states a condition that must hold at that point. The compiler removes assertions by default, so they cost nothing in a release build; -Sa keeps them. A failed assertion prints the message and the source position and exits with status 227.

assertion.pas
program assertion;
{$mode objfpc}{$H+}

var
  Age: Integer;

begin
  Age := 15;
  Assert(Age >= 18, 'Age must be at least 18');
  WriteLn('accepted');
end.

Without -Sa the program prints accepted and exits normally. Assertions are for programming errors, not for validating user input: a bad command-line argument should produce a message and a non-zero exit code, while a broken invariant can stop the program immediately.

$ fpc -gl -Sa assertion.pas
$ ./assertion
Age must be at least 18 (assertion.pas, line 7).

$ echo $?
227

Heap leaks with heaptrc

-gh links the heaptrc unit, which records every allocation and prints a report at program exit. The program below allocates an integer with New and never disposes it, so the report has one unfreed block.

leak.pas
program leak;
{$mode objfpc}{$H+}

var
  P: ^Integer;

begin
  New(P);
  P^ := 42;
  WriteLn('P^ = ', P^);
end.

The report names the executable and lists allocated, freed, and unfreed blocks, followed by a call trace for each leak. The trace is written to standard error, so it appears after the program's own output. A healthy run ends with 0 unfreed memory blocks. The first line of the report contains the executable path and the call trace is long, so both are filtered out of the transcript below; the heap sizes depend on the platform.

$ fpc -gl -gh leak.pas
$ ./leak 2>&1 | grep -v -e '^Heap dump' -e '^Call trace'
P^ = 42
1 memory blocks allocated : 4/8
0 memory blocks freed     : 0/0
1 unfreed memory blocks : 4
True heap size : 32768
True free heap : 32544
Should be : 32568

Debugging with gdb

A nil dereference is caught by the Pascal runtime and reported as runtime error 216, an access violation, together with the source line. That names the failing statement but not the chain of calls that led to it. Compile with -gl -O- to keep line information and disable optimization, then let gdb run the program and print a backtrace. The program below dereferences a nil pointer after flushing its output.

crash.pas
program crash;
{$mode objfpc}{$H+}

var
  P: ^Integer;

begin
  P := nil;
  WriteLn('before the crash');
  Flush(Output);
  WriteLn(P^);
end.

Without a debugger the same run stops with runtime error 216 and the source line. Under gdb the signal reaches the debugger before the Pascal handler, so the output shows the machine-level fault and one frame per call. The backtrace points at line 9 and names main, the program's main block. The Flush(Output) is necessary to see the message before the crash: without it the buffered text is lost when the process dies.

$ fpc -gl -O- crash.pas
$ ./crash
before the crash
Runtime error 216 at $0000000000401122
  $0000000000401122  main,  line 9 of crash.pas
  $0000000000422F2C

$ gdb -q -batch -ex run -ex bt ./crash
before the crash

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401122 in main () at crash.pas:9
9	  WriteLn(P^);
#0  0x0000000000401122 in main () at crash.pas:9

The flags that matter

-gl adds line information for runtime errors and the debugger. -Cr, -Co, -Sa, and -gh enable range checks, overflow checks, assertions, and the heap report. -O- disables optimization; Free Pascal has no -O0, so the dash is the correct spelling. -vw- hides warnings, -vn- hides notes, and -vw-n- hides both, which is useful with the stock generics units that emit notes in 3.2.2.

-FU sets the directory for unit output, and -Fu adds a search path for units. -B forces a full rebuild, and -Xs strips the symbol table from the executable. Compile the same program with and without checks in two build targets: checks for development, optimization and no checks for the release binary.

Source

Free Pascal: compiler options

Free Pascal: heaptrc unit

GDB documentation

In this article we built a program from a unit with a Makefile, inspected the compiled unit with ppudump, enabled range, overflow, assertion, and heap checks, and located a crash in gdb. The unit testing article turns these checks into a repeatable suite, and the style guide collects the conventions that keep a build clean.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.