Name
$IfDef Compiler Directive
Syntax
{$IfDef Name
}
...
{$Else}
...
{$Endif}
Scope
Local
Description
$IfDef
starts a
conditional compilation section. If Name
is defined (by a prior occurrence of $Define
), the
source code after the $IfDef
directive is
compiled, up to the corresponding $Else
or
$EndIf
compiler directive. The use of
$Else
is optional. You can nest conditional
compilation sections.
A common use for conditional compilation is to test the version of Delphi. Table 8-1 lists the symbols predefined by the compiler with the version of the compiler that defines each symbol.
Symbol |
Compiler Version |
|
Defined for |
|
All versions |
|
Delphi 1 |
|
Delphi 2 |
VER93 |
C++ Builder 1 |
|
Delphi 3 |
|
C++ Builder 3 |
|
Delphi 4 |
|
C++ Builder 4 |
|
Delphi 5 |
|
Delphi 1 |
|
Delphi 2, 3, 4, 5; C++ Builder 1, 3, 4 |
Example
{$IfOpt D+} // Debugging is enabled. {$define DEBUG} {$Assertions On} {$RangeChecks On} {$OverflowChecks On} {$else} {$undef DEBUG} // Just in case it's defined in the project options {$endif} ... {$ifdef DEBUG} Log('Debug message'); {$endif}
See Also
$Define Compiler Directive, $Else Compiler Directive, $Endif Compiler Directive, $IfNDef Compiler Directive, $IfOpt Compiler Directive, $Undef Compiler Directive |
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.