Delphi Compiler Version symbols or constants used with IFDEF - ENDIF

If you plan on writing Delphi code that should work with several versions of the Delphi compiler then we first need to know under which versions our code gets compiled. Suppose you are writing your own (commercial) custom component. Users of your component might have different Delphi versions than you have. If they try to recompile the component's code - they might be in trouble! What if you were using default parameters in your functions and the user has Delphi 3?


These are symbols that can be used in $IFDEF /$ENDIF conditional compilation directives.
{$IFDEF VER40}  - Turbo pascal 4
{$IFDEF VER50}  - Turbo pascal 5
{$IFDEF VER55}  - Turbo pascal 5.5
{$IFDEF VER60}  - Turbo pascal 6
{$IFDEF VER70}  - Borland pascal 7 (And turbo pascal 1.5 for windows)
{$IFDEF VER80}  - Delphi 1
{$IFDEF VER90}  - Delphi 2
{$IFDEF VER100} - Delphi 3
{$IFDEF VER120} - Delphi 4
{$IFDEF VER130} - Delphi 5
{$IFDEF VER140} - Delphi 6
{$IFDEF VER150} - Delphi 7
{$IFDEF VER160} - Delphi 8
{$IFDEF VER170} - Delphi 2005
{$IFDEF VER180} - Delphi 2006
{$IFDEF VER180} - Delphi 2007
{$IFDEF VER185} - Delphi 2007
{$IFDEF VER200} - Delphi 2009
{$IFDEF VER210} - Delphi 2010
{$IFDEF VER220} - Delphi XE
{$IFDEF VER230} - Delphi XE2
{$IFDEF VER240} - Delphi XE3
{$IFDEF VER250} - Delphi XE4
{$IFDEF VER260} - Delphi XE5
{$IFDEF VER265} - Appmethod 1.0
{$IFDEF VER270} - Delphi XE6
{$IFDEF VER280} - Delphi XE7
{$IFDEF VER290} - Delphi XE8
{$IFDEF VER300} - Delphi 10 Seattle
{$IFDEF VER310} - Delphi 10.1 Berlin

Example
{$IFDEF VER150}
    //some delphi 7-specific code here
{$ENDIF}
{$IFDEF VER210}
    //some delphi 2010-specific code here
{$ENDIF}
{$IFDEF VER310}
    //some delphi 10-specific code here
{$ENDIF}

CompilerVersion constant
The CompilerVersion constant was introduced in Delphi 6 along with conditional expressions. The CompilerVersion constant identifies the internal version number of the Delphi compiler. It is defined in the System unit and may be referenced either in code just as any other constant.

We can use CompilerVersion constant in following ways...
if CompilerVersion = 15 then
   sCompilerName := 'Delphi 7';
if CompilerVersion = 20 then
   sCompilerName := 'Delphi 2009';
if CompilerVersion = 30 then
   sCompilerName := 'Delphi 10 Seattle';
or 
in conditional compiler expressions:
{$if CompilerVersion > 18} 
    // some code only compiled for Delphi 2007 or later 
{$ifend}
{$if CompilerVersion >= 25} 
    // some code only compiled for Delphi XE4 or later 
{$ifend}

Conditional Define constants for Windows, Linux and Delphi
To write different code in a single file that targets Windows, Linux and Delphi you could use this type of conditional compilation block:
{$IFDEF LINUX}
  //do Linux-specific code here
{$ENDIF}
{$IFDEF MSWINDOWS}
  //do Windows-specific code here
{$ENDIF}
{$IFDEF CLR}
  //do .NET-specific code here
{$ENDIF}
Or alternatively this form, which is functionally equivalent:
{$IF Defined(LINUX)}
  //do Linux-specific code here
{$ELSEIF Defined(MSWINDOWS)}
  //do Windows-specific code here
{$ELSEIF Defined (CLR)}
  //do .NET-specific code here
{$IFEND}

Comments

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Variants in Delphi. Use of Variant Array. How to check a Variant is unassigned or empty or clear ?