Common Predefined Symbols in the Free Pascal Compiler

Notes

Someone in a Lazarus community group asked how to detect the target CPU architecture. I pointed them to the official documentation and collected the relevant compiler defines here as a quick reference for Free Pascal developers.

The canonical reference is the Free Pascal manual’s Compiler defines during compilation. The symbols available depend on the compiler version and target platform, so check the manual for your version when a conditional matters. If you are using FPC 3.3.1, see the visibility-check note in the Chinese version of this blog.

Free Pascal provides predefined symbols that let a program select code at compile time. For example, CPUX86_64 is defined when the target CPU is x86-64:

{$IFDEF CPUX86_64}
  // This code is compiled only for an x86-64 target.
{$ENDIF}

Target platform

CPU architecture

The following CPU defines are listed in the compiler manual:

Define When it is defined
CPU86 The target is an Intel 80x86-compatible processor (16 or 32 bit).
CPU87 The target is an Intel 80x86-compatible processor (16 or 32 bit).
CPU386 The target is an Intel 80386 or later.
CPUI386 The target is an Intel 80386 or later.
CPU68K The target is a Motorola 680x0-compatible processor.
CPUM68K The target is a Motorola 680x0-compatible processor.
CPUM68020 The target is a Motorola 68020 or later.
CPU68 The target is a Motorola 680x0-compatible processor.
CPUSPARC32 The target is SPARC v7 or compatible.
CPUSPARC The target is SPARC v7 or compatible.
CPUALPHA The target is Alpha AXP or compatible.
CPUPOWERPC The target is a 32- or 64-bit PowerPC-compatible processor.
CPUPOWERPC32 The target is a 32-bit PowerPC-compatible processor.
CPUPOWERPC64 The target is a 64-bit PowerPC-compatible processor.
CPUX86_64 The target is AMD64 or Intel 64.
CPUAMD64 The target is AMD64 or Intel 64.
CPUX64 The target is AMD64 or Intel 64.
CPUIA64 The target is a 64-bit Intel Itanium processor.
CPUARM The target is a 32-bit ARM processor.
CPUAVR The target is a 16-bit AVR processor.
CPU16 The target CPU is 16 bit.
CPU32 The target CPU is 32 bit.
CPU64 The target CPU is 64 bit.
CPUI8086 The target is 16-bit x86 (i8086).

Operating system

These are examples of operating-system defines documented by Free Pascal. Some targets define more than one symbol:

Target OS Defines
AIX AIX, UNIX
Amiga AMIGA
Android ANDROID
Linux LINUX, UNIX
FreeBSD FREEBSD, BSD, UNIX, DRAGONFLY
NetBSD NETBSD, BSD, UNIX
OpenBSD OPENBSD, BSD, UNIX
SunOS SUNOS, SOLARIS, UNIX
go32v2 GO32V2, DPMI
16-bit MS-DOS MSDOS (FPC 2.7.1 and later)
Haiku HAIKU
OS/2 OS2
NetWare NETWARE, NETWLIBC
Nintendo DS NDS
emx OS2, EMX
Solaris SOLARIS
Wii WII
Native Windows NT NATIVENT
16-bit Windows WIN16
Windows (all) WINDOWS
32-bit Windows WIN32, MSWINDOWS
64-bit Windows WIN64, MSWINDOWS
Windows CE WINCE, UNDER_CE, FPC_OS_UNICODE
Classic Amiga AMIGA
Atari TOS ATARI
Classic Macintosh MACOS
PalmOS PALMOS
BeOS BEOS, UNIX
QNX RTP QNX, UNIX
Mac OS X BSD, DARWIN, UNIX, IPHONESIM

For example, this code is included only for a Windows target:

{$IFDEF MSWINDOWS}
  // Windows-specific code
{$ENDIF}

Compiler information

The compiler manual also lists these defines:

Define Meaning
FPC_LINK_DYNAMIC The output is dynamically linked; defined when using -XD.
FPC_LINK_STATIC The output is statically linked; the manual lists this as the default mode.
FPC_LINK_SMART The output is smartlinked; defined when using -XX.
FPC_PROFILE Profiling code is added with the -pg compiler switch.
FPC_CROSSCOMPILING The target OS or CPU differs from the source OS or CPU.
FPC Always defined by Free Pascal.
VER2, VER2_0, VER2_2 Identify Free Pascal 2.x, 2.0.x, and 2.2.x respectively.
VER3, VER3_0, VER3_2 Identify Free Pascal 3.x, 3.0.x, and 3.2.x respectively.
FPC_VERSION The major FPC version number.
FPC_RELEASE The minor FPC version number.
FPC_PATCH The patch version number.
FPC_FULLVERSION The full version as one number; for example, FPC 2.2.4 uses 20204.
ENDIAN_LITTLE The target is little-endian. The manual gives x86, Alpha, and ARM as examples.
ENDIAN_BIG The target is big-endian. The manual gives 680x0, PowerPC, SPARC, and MIPS as examples.
FPC_DELPHI Delphi mode is active via -MDelphi or $MODE DELPHI.
FPC_OBJFPC ObjFPC mode is active via -Mobjfpc or $MODE OBJFPC.
FPC_TP Turbo Pascal mode is active via -Mtp or $MODE TP.
FPC_MACPAS Mac Pascal mode is active via -Mmacpas or $MODE MACPAS.

Checking compiler options

The {$IFOPT} directive tests whether a compiler option is enabled. The following example includes the statement only when the D+ debug option is active:

{$IFOPT D+}
  writeln('Debug mode is enabled');
{$ENDIF}

You can also define your own DEBUG symbol when that option is active:

{$IFOPT D+}
  {$DEFINE DEBUG}
{$ENDIF}

{$IFDEF DEBUG}
  writeln('DEBUG is defined');
{$ENDIF}

This lets you include diagnostic code conditionally. The exact build mode still depends on how the project or IDE invokes the compiler.

Series: Free Pascal and Lazarus