RAD Studio (Common)
ContentsIndex
PreviousUpNext
Conditional compilation (Delphi)

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 Delphi 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. The following standard conditional symbols are defined: 

VER<nnn> Always defined, indicating the version number of the Delphi compiler. (Each compiler version has a corresponding predefined symbol. For example, compiler version 18.0 has VER180 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. 

CLR Indicates the code will be compiled for the .NET platform. 

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. 

For example, to find out the version of the compiler and run-time library that was used to compile your code, you can use $IF with the CompilerVersion, RTLVersion and other constants:

{$IFDEF CONDITIONALEXPRESSIONS} 
   {$IF CompilerVersion >= 17.0}  
     {$DEFINE HAS_INLINE}  
   {$IFEND} 
   {$IF RTLVersion >= 14.0}
     {$DEFINE HAS_ERROUTPUT} 
   {$IFEND} 
{$ENDIF}

Note: Conditional symbols are not Delphi identifiers and cannot be referenced in actual program code. Similarly, Delphi 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.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!