Show: Delphi C++
Display Preferences

Conditional compilation (Delphi)

From RAD Studio XE3
Jump to: navigation, search

Go Up to Delphi Compiler Directives (List) Index

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

Contents

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 field on the Project > Options > Delphi Compiler page.

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, the following Delphi code snippet processes differently depending on whether the DEBUG conditional define is set. You can set conditional defines on the Project > Options > Delphi Compiler page or by entering {$DEFINE DEBUG} in your code:

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


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 that everything in your project reflects the current status of conditional symbols.

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.

Predefined Conditionals

The following standard conditional symbols are defined:

Category Symbol DCC32 DCC64 DCCOSX Comments
Compiler DCC DEFINED DEFINED DEFINED
VER230

For a list of compiler versions, see Compiler Versions.
DEFINED DEFINED DEFINED The VER<nnn> conditional is always defined, indicating the version number of the Delphi compiler. Each compiler version has a corresponding predefined symbol. For example, compiler version 23.0 (Delphi XE2) has VER230 defined.)
CONSOLE DEFINED DEFINED DEFINED Defined if an application is being compiled as a console application.
Platform NATIVECODE DEFINED DEFINED DEFINED since Delphi.Net
MSWINDOWS DEFINED DEFINED not defined Indicates that the operating environment is Windows. Use MSWINDOWS to test for any flavor of the Windows platform instead of WIN32.
WIN32 DEFINED not defined not defined Target platform is the native 32-bit Windows platform.
WIN64 not defined DEFINED not defined Target platform is 64-bit Windows. *New* at XE2/x64
MACOS not defined not defined DEFINED Target platform is Mac OS X. *New* at XE2/osx
MACOS32 not defined not defined DEFINED Target platform is 32-bit Mac OS X. *New* at XE2/osx
POSIX not defined not defined DEFINED since Kylix
POSIX32 not defined not defined DEFINED since Kylix
CPU CPU386 DEFINED not defined DEFINED Indicates that the CPU is an Intel 386 or later.
CPUX86 DEFINED not defined DEFINED CPU is an Intel 386 or later on any platform. *New* at XE2/x64
CPUX64 not defined DEFINED not defined The CPU is in a 64-bit environment, such as DCC64.EXE. *New* at XE2/x64
Availability ASSEMBLER DEFINED DEFINED DEFINED Assembler syntax is accepted.
UNICODE DEFINED DEFINED DEFINED UNICODE is defined as the default string type.
CONDITIONALEXPRESSIONS DEFINED DEFINED DEFINED Tests for the use of the $IF directive.
ALIGN_STACK not defined not defined DEFINED Defined in code that may be shared with the OS X compiler and another compiler on another platform such as Linux that does not have a rigid stack alignment requirement. For more information, see Eli Boling's blog at http://blogs.embarcadero.com/eboling/2009/05/20/5607 . *New* at XE2/osx
PC_MAPPED_EXCEPTIONS not defined not defined DEFINED Defined when compiling on a platform or for a target platform that uses address maps instead of stack frames to unwind exceptions (such as OS X). *New* at XE2.
PIC never never DEFINED if available Defined for platforms that require Position-Independent Code (PIC), such as OS X.
In the table column heads, DCC32 is the 32-bit Windows Delphi compiler; DCC64 is the 64-bit Windows Delphi compiler; DCCOSX is the Mac OS X Delphi compiler.

Using Conditional Defines for the Compiler Version

For example, to determine the version of the compiler and run-time library that were 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}

See the table of Compiler Versions for a list of version numbers associated with various released Delphi compilers.

Predefined Constants

Constants can be more powerful than conditionals because you can use constants programmatically in Delphi code. Conditionals, on the other hand, are accepted only inside conditional compiler directives such as {$IF} and {$IFDEF}.

There are two important constants available:

  • System.RTLVersion is a constant defined as the version of the run-time library. For XE2, RTLVersion is 23.
  • System.CompilerVersion is a constant defined as the version of the current Delphi compiler. For XE2, CompilerVersion is 23.
  • System.FireMonkeyVersion is defined as the version of the current FireMonkey library. For XE2 RTM release, FireMonkeyVersion is 16.0. For XE2 Update 2, FireMonkeyVersion is 16.1.

See Also

Personal tools
Previous Versions
In other languages