Delphi Compiler Version Directives

Group of coworkers gathered around a computer

gilaxia / Getty Images

If you plan on writing Delphi code that should work with several version of the Delphi compiler you need to know under which versions your 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—your code—they might be in trouble! What if you were using default parameters in your functions and the user has Delphi 3?

Compiler directive: $IfDef

Compiler directives are special syntax comments we can use to control the features of the Delphi compiler. The Delphi compiler has three types of directives: switch directives, parameter directives, and conditional directives. Conditional compilation lets us selectively compile parts of a source code depending on which conditions are set.

The $IfDef compiler directive starts a conditional compilation section.

The syntax looks like:

 {$IfDef DefName}

 ...

 {$Else}

 ...

 {$EndIf}

 

The DefName presents the so-called conditional symbol. Delphi defines several standard conditional symbols. In the "code" above, if the DefName is defined the code above $Else gets compiled.

Delphi Version Symbols

A common use for the $IfDef directive is to test the version of the Delphi compiler. The following list indicates the symbols to check when compiling conditionally for a particular version of the Delphi compiler:

  • SYMBOL - COMPILER VERSION
  • VER80 - Delphi 1
  • VER90 - Delphi 2
  • VER100 - Delphi 3
  • VER120 - Delphi 4
  • VER130 - Delphi 5
  • VER140 - Delphi 6
  • VER150 - Delphi 7
  • VER160 - Delphi 8
  • VER170 - Delphi 2005
  • VER180 - Delphi 2006
  • VER180 - Delphi 2007
  • VER185 - Delphi 2007
  • VER200 - Delphi 2009
  • VER210 - Delphi 2010
  • VER220 - Delphi XE
  • VER230 - Delphi XE2
  • WIN32 - Indicates that the operating environment is the Win32 API.
  • LINUX - Indicates that the operating environment is Linux
  • MSWINDOWS - Indicates that the operating environment is the MS Windows/li]
  • CONSOLE - Indicates that an application is being compiled as a console application

By knowing the above symbols it is possible to write code which works with several versions of Delphi by using compiler directives to compile appropriate source code for each version.

Note: symbol VER185, for example, is used to indicate Delphi 2007 compiler or an earlier version.

Using "VER" symbols

It's quite usual (and desirable) for each new Delphi version to add several new RTL routines to the language.

For example, the IncludeTrailingBackslash function, introduced in Delphi 5, adds "\" to the end of a string if it is not already there. In the Delphi MP3 project, I have used this function and several readers have complained that they can't compile the project—they have some Delphi version prior to Delphi 5.

One way to solve this problem is to create your own version of this routine - the AddLastBackSlash function. If the project should be compiled on Delphi 5, the IncludeTrailingBackslash is called. If some of the previous Delphi versions are used, then we simulate the IncludeTrailingBackslash function.

It could look something like:

 function AddLastBackSlash(str: string) : string;

begin{$IFDEF VER130}

  Result:=IncludeTrailingBackslash(str) ;

 {$ELSE}
if Copy(str, Length(str), 1) = "\" then
    Result := str

  else

   
Result := str + "\";​
{$ENDIF}end;

When you call the AddLastBackSlash function Delphi figures out which portion of the function should be used and the other part is simply skipped.

Delphi 2008

Delphi 2007 uses VER180 in order to maintain non-breaking compatibility with Delphi 2006 and then adds VER185 in order for development that specifically needs to target Delphi 2007 for whatever reason. Note: any time the interface of a unit changes the code that uses that unit has to be re-compiled.

Delphi 2007 is non-breaking release meaning that DCU files from Delphi 2006 will work as-is.

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Delphi Compiler Version Directives." ThoughtCo, Jul. 30, 2021, thoughtco.com/delphi-compiler-version-directives-1058183. Gajic, Zarko. (2021, July 30). Delphi Compiler Version Directives. Retrieved from https://www.thoughtco.com/delphi-compiler-version-directives-1058183 Gajic, Zarko. "Delphi Compiler Version Directives." ThoughtCo. https://www.thoughtco.com/delphi-compiler-version-directives-1058183 (accessed March 19, 2024).