Conditional compilation

 

Conditional compilation is based on the existence and evaluation of constants, the status of compiler switches, and the definition of conditional symbols.

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. The $DEFINE directive sets a specified symbol to true, and the $UNDEF directive sets it to false. You can also define a conditional symbol by using the -D switch with the command-line compiler or by adding the symbol to the Conditional Defines box on the Directories/Conditionals page of the Project|Options dialog.

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 Pascal identifiers. $IFOPT compiles or suppresses code depending on whether a specified compiler switch is enabled.

For example,

{$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}

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. Delphi defines the following standard conditional symbols.

 

VER140               Always defined, indicating that this is version 14.0 of the Object Pascal compiler. (Each compiler version has a corresponding predefined symbol. For example, version 10.0 has VER100 defined.)

MSWINDOWS   Indicates that the operating environment is Windows. Use MSWINDOWS to test for any flavor of the Windows platform instead of WIN32. .

WIN32                 Indicates that the operating environment is the Win32 API. Use WIN32 for distinguishing between specific Windows platforms, such as 32-bit versus 64-bit Windows. In general, don't limit code to WIN32 unless you know for sure that the code will not work in WIN64. Use MSWINDOWS instead.

LINUX                  Indicates that the operating environment is Linux.

CPU386              Indicates that the CPU is an Intel 386 or better.

CONSOLE           Defined if an application is being compiled as a console application.

CONDITIONALEXPRESSIONS    Tests for the use of $IF directives.

 

Note:          An important conditional symbol that is NOT defined is LINUX. This is defined only on the Linux version of the compiler, and can be used to protect platform-specific code in cross-platform applications.

 

Conditional symbols are not Pascal identifiers and cannot be referenced in actual program code. Similarly, 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 everything in your project reflects the current status of conditional symbols.

 

See also

Compiler directives (list)

DEFINE directive

ELSE

ELSELF

ENDIF

IF directive

IFDEF directive

IFEND directive

IFNDEF directive

IFOPT directive

UNDEF directive