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 |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access