Sunday, October 20, 2019

Delphi Compiler Version Directives

Delphi Compiler Version Directives 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 components 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 VERSIONVER80 - Delphi 1VER90 - Delphi 2VER100 - Delphi 3VER120 - Delphi 4VER130 - Delphi 5VER140 - Delphi 6VER150 - Delphi 7VER160 - Delphi 8VER170 - Delphi 2005VER180 - Delphi 2006VER180 - Delphi 2007VER185 - Delphi 2007VER200 - Delphi 2009VER210 - Delphi 2010VER220 - Delphi XEVER230 - Delphi XE2WIN32 - Indicates that the operating environment is the Win32 API.LINUX - Indicates that the operating environment is LinuxMSWINDOWS - 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 Its 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 cant 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.