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.

Table 8-1. Predefined Symbols

Symbol

Compiler Version

CONSOLE

Defined for {$AppType Console}

CPU386

All versions

VER80

Delphi 1

VER90

Delphi 2

VER93

C++ Builder 1

VER100

Delphi 3

VER110

C++ Builder 3

VER120

Delphi 4

VER125

C++ Builder 4

VER130

Delphi 5

WINDOWS

Delphi 1

WIN32

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.