Dsm
Dsm

Reputation: 6013

Version dependent compiles - $ENDIF and $IFEND

I seem to be in a catch 22 situation. I want to add compiler version dependant code. OK - that is pretty standard. But the syntax of the $IF statements is different between versions.

Here is what I am trying to achieve

{$IF CompilerVersion = 28}
  if (fPendingObject = pObject) and (Addr(fPendingActionEvent) = Addr(pPendingActionEvent) ) then
{$ELSE}
  if (fPendingObject = pObject) and (@fPendingActionEvent = @pPendingActionEvent ) then
{$ENDIF}

This compiles in Delphi XE7, but not in Seattle or Berlin. Those compilers require the syntax

{$IF CompilerVersion = 28}
  if (fPendingObject = pObject) and (Addr(fPendingActionEvent) = Addr(pPendingActionEvent) ) then
{$ELSE}
  if (fPendingObject = pObject) and (@fPendingActionEvent = @pPendingActionEvent ) then
{$IFEND}  

($IFEND instead of $ENDIF). But XE7 won't accept that syntax.

There obviously must be a trick, and indeed the Delphi 2009 documentation says so, but my poor brain can't work out the trick. Can someone help?

Upvotes: 2

Views: 1787

Answers (1)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

There is a compiler option that allows the use of the older {$IFEND} directive:

  • Project
    • Options
      • Delphi Options
      • Compiling
        • Require $IF to be terminated by $IFEND: [x] true

Then there is the {$LEGACYIFEND ON|OFF} directive, which does the same, locally. Setting it on will make XE7 accept {$IFEND}, like in the older versions. I often use something like:

// For Delphi XE3 and up:
{$IF CompilerVersion >= 24.0 }
  {$LEGACYIFEND ON}
{$IFEND}

Obviously, this option is on by default in your Seattle or Berlin projects, but not in XE7. You can turn it off or on, depending on your preferences.

Upvotes: 5

Related Questions