Conditional compilation (Object Pascal)
Go Up to Object Pascal Compiler Directives (List) Index
Conditional compilation is based on the existence and evaluation of constants, the status of compiler switches, and the definition of conditional symbols.
Contents
Conditional symbols work like Boolean variables: they are either defined (True) or undefined (False). Any valid conditional symbol is treated as false until it has been defined.
You can define a conditional in the following ways:
- Use the
{$DEFINE}
directive to set a specified symbol to True, and the{$UNDEF}
directive to set the symbol to False. - Use the
-D
switch with the command-line compiler (this option is supported by all the Object Pascal compilers). - Add the symbol to the Conditional Defines field on the Project > Options > Object Pascal Compiler page.
The conditional directives {$IFDEF}
, {$IFNDEF}
, {$IF}
, {$ELSEIF}
, {$ELSE}
, {$ENDIF}
, and {$IFEND}
allow you to compile or suppress code based on the status of a conditional symbol. {$IF}
and {$ELSEIF}
allow you to base conditional compilation on declared Object Pascal identifiers. {$IFOPT} compiles or suppresses code depending on whether a specified compiler switch is enabled.
For example, the following Object Pascal code snippet processes differently depending on whether the DEBUG conditional define is set ({$DEFINE DEBUG}
):
{$DEFINE DEBUG}
{$IFDEF DEBUG}
Writeln('Debug is on.'); // This code executes.
{$ELSE}
Writeln('Debug is off.'); // This code does not execute.
{$ENDIF}
{$UNDEF DEBUG}
{$IFNDEF DEBUG}
Writeln('Debug is off.'); // This code executes.
{$ENDIF}
Note: Conditional symbols are not Object Pascal identifiers and cannot be referenced in actual program code. Similarly, Object Pascal identifiers cannot be referenced in any conditional directives other than
{$IF}
and{$ELSEIF}
.
Note: Conditional definitions are evaluated only when source code is recompiled. If you change a conditional symbol's status and then rebuild a project, source code in unchanged units may not be recompiled. Use Project > Build All Projects to ensure that everything in your project reflects the current status of conditional symbols.
Conditional-directive constructions can be nested up to 32 levels deep. For every {$IFxxx}
, the corresponding {$ENDIF}
or {$IFEND}
must be found within the same source file. Conditional symbols must start with a letter, followed by any combination of letters, digits, and underscores; they can be of any length, but only the first 255 characters are significant.
Predefined Conditionals
The following standard conditional symbols are defined:
Category | Symbol | Win32 | Win64 | Mac OS X | iOS Device | iOS Simulator | Android | Comments |
---|---|---|---|---|---|---|---|---|
Compiler | DCC | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | |
VER280 For a list of compiler versions, see Compiler Versions. |
DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | ||
Platform | CONSOLE | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | Defined if an application is being compiled as a console application. |
IOS | not defined | not defined | not defined | DEFINED | DEFINED | N/A | Defined if the target platform is iOS. | |
NATIVECODE | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | Since Object Pascal.Net | |
MSWINDOWS | DEFINED | DEFINED | not defined | not defined | not defined | not defined | Indicates that the operating environment is Windows. Use MSWINDOWS to test for any flavor of the Windows platform instead of WIN32. | |
WIN32 | DEFINED | not defined | not defined | not defined | not defined | not defined | Target platform is the native 32-bit Windows platform. | |
WIN64 | not defined | DEFINED | not defined | not defined | not defined | not defined | Target platform is 64-bit Windows. | |
MACOS | not defined | not defined | DEFINED | DEFINED | DEFINED | not defined | Target platform is Mac OS X. | |
MACOS32 | not defined | not defined | DEFINED | DEFINED | DEFINED | not defined | Target platform is 32-bit Mac OS X. | |
LINUX | not defined | not defined | not defined | not defined | not defined | not defined | Since Kylix | |
LINUX32 | not defined | not defined | not defined | not defined | not defined | not defined | Since Kylix | |
POSIX | not defined | not defined | DEFINED | DEFINED | DEFINED | DEFINED | Since Kylix | |
POSIX32 | not defined | not defined | DEFINED | DEFINED | DEFINED | DEFINED | Since Kylix | |
ANDROID | not defined | not defined | not defined | not defined | not defined | DEFINED | Defined if the target platform is Android. | |
CPU | CPUARM | not defined | not defined | not defined | DEFINED | not defined | DEFINED | Defined if the CPU is based on the ARM architecture, such as the Object Pascal mobile compiler for the iOS device (DCCIOSARM.EXE). |
CPU386 | DEFINED | not defined | DEFINED | not defined | DEFINED | not defined | Indicates that the CPU is an Intel 386 or later. | |
CPUX86 | DEFINED | not defined | DEFINED | not defined | DEFINED | not defined | CPU is an Intel 386 or later on any platform. | |
CPUX64 | not defined | DEFINED | not defined | not defined | not defined | not defined | The CPU is in a 64-bit environment, such as DCC64.EXE. | |
Availability | ||||||||
ALIGN_STACK | not defined | not defined | DEFINED | not defined | DEFINED | not defined | Defined in code that may be shared with the OS X compiler and another compiler on another platform such as Linux that does not have a rigid stack alignment requirement. | |
ASSEMBLER | DEFINED | DEFINED | DEFINED | not defined | DEFINED | not defined | Assembler syntax is accepted. | |
AUTOREFCOUNT | not defined | not defined | not defined | DEFINED | DEFINED | DEFINED | Defined for compilers that use automatic reference counting, such as the Object Pascal mobile compilers. | |
EXTERNALLINKER | not defined | not defined | not defined | DEFINED | not defined | DEFINED | Defined for compilers that have an external linker and the LLVM code generator; the Object Pascal mobile compilers have the external ld linker and use LLVM as code generator. | |
UNICODE | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | UNICODE is defined as the default string type. | |
CONDITIONALEXPRESSIONS | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | DEFINED | Tests for the use of the $IF directive. | |
ELF | not defined | not defined | not defined | +DEFINED+ | not defined | not defined | Defined when targeting Executable and Linkable Format (ELF) files. | |
NEXTGEN | not defined | not defined | not defined | DEFINED | DEFINED | DEFINED | Defined for compilers (such as the Object Pascal mobile compilers) that use "next-generation" language features, such as 0-based strings. | |
PC_MAPPED_EXCEPTIONS | not defined | not defined | DEFINED | not defined | DEFINED | not defined | Defined when compiling on a platform or for a target platform that uses address maps instead of stack frames to unwind exceptions (such as OS X). | |
PIC | never | never | DEFINED if -P is specified | never | DEFINED if -P is specified | always defined | Defined for platforms that require Position-Independent Code (PIC), such as OS X. | |
UNDERSCOREIMPORTNAME | DEFINED | not defined | DEFINED | not defined | DEFINED | not defined | Defined for compilers that add a leading underscore (for example, in names of dynamic libraries imported from Mac OS). | |
WEAKREF | not defined | not defined | not defined | DEFINED | DEFINED | DEFINED | Defined for compilers that can use weak references (the [weak] attribute).
| |
WEAKINSTREF | not defined | not defined | not defined | DEFINED | DEFINED | DEFINED | Defined when weak references are defined for instances. | |
WEAKINTREF | not defined | not defined | not defined | DEFINED | DEFINED | DEFINED | Defined when weak references are defined for interfaces. |
Using Conditional Defines for the Compiler Version
For example, to determine the version of the compiler and run-time library that were used to compile your code, you can use {$IF}
with the CompilerVersion, RTLVersion and other constants:
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 19.5}
{$DEFINE HAS_INLINE}
{$IFEND}
{$IF RTLVersion >= 20.0}
{$DEFINE HAS_ERROUTPUT}
{$IFEND}
{$ENDIF}
Predefined Constants
Constants can be more powerful than conditionals because you can use constants programmatically in Object Pascal code. Conditionals, on the other hand, are accepted only inside conditional compiler directives such as {$IF}
and {$IFDEF}
.
There are three important constants available:
- System.RTLVersion is a constant defined as the version of the run-time library. For Appmethod 1.15, RTLVersion is 28.
- System.CompilerVersion is a constant defined as the version of the current Object Pascal compiler. For Appmethod 1.15, CompilerVersion is 28.
- FMX.Types.FireMonkeyVersion is a constant defined as the version of the current FireMonkey library. For Appmethod 1.15, FireMonkeyVersion is 21.0.
See Also
- Object Pascal Compiler Directives (List) Index
- Compiler Versions
- CompilerVersion (Object Pascal) Code Example
- IF directive (Object Pascal) and IFEND directive (Object Pascal)
- IFDEF directive (Object Pascal) and ENDIF directive (Object Pascal)
- Declarations and Statements
- Object Pascal Compiler page in Project Options