Compiler directive is not set in Delphi 10.2 - delphi

I have the following unit implementation in my delphi probject.
uses
{$IFDEF Ver270} JSON, {$ELSE} DBXJSON, {$ENDIF}
In Delphi XE4 DBXJSON will be implemented - that's fine.
In Delphi XE6 JSON will be implemented - that's fine too.
But in Delphi 10.2, DBXJSON will be implemented - not JSON. Why? Is this a bug in Delphi 10.2?

This is not a bug, it is by design. Each version has exactly one VERXXX definition. VER270 is defined in XE6 and XE6 only. For version 10.2 VER320 is defined.
In your scenario it is much simpler to use code like this:
uses
{$IF RTLVersion >= 27} JSON, {$ELSE} DBXJSON, {$IFEND}
Another option is to use a standard include file like jedi.inc. This takes the pain out of such conditional statements. If you use jedi.inc then you can code it like this:
uses
{$IFDEF DELPHIXE6_UP} JSON, {$ELSE} DBXJSON, {$ENDIF}

Related

Delphi CompilerVersion directive issues

I have a library code file which is shared between Delphi 5 and DelphiXE2. I am attempting to add anonymous method functionality to the code file, but only for DelphiXE2 projects (since Delphi 5 doesn't support anonymous methods). It seemed I should be able to use the CompilerVersion (Note: I don't want to limit it to DelphiXE2, just in case we ever upgrade).
{$IF CompilerVersion >= 23}
{$DEFINE AnonymousAvail}
{$IFEND}
This worked nicely in XE2, but it turns out, Delphi 5 doesn't support the $IF directive. I decided to wrap it in an $IFDEF. This worked nicely in Delphi 5, but XE2 also doesn't seem to have CompilerVersion defined, so AnonymousAvail is not defined.
{$IFDEF CompilerVersion}
{$IF CompilerVersion >= 23}
{$DEFINE AnonymousAvail}
{$IFEND}
{$ENDIF}
Any help would be appreciated.
Note: I cannot move anonymous method code to a different code file.
Do what the documentation says:
{$IFDEF ConditionalExpressions}
{$IF CompilerVersion >= 23.0}
{$DEFINE AnonymousAvailable}
{$IFEND}
{$ENDIF}
Be sure that the outer condition is as shown (and closed with ENDIF) and you can use CompilerVersion and other constants and expressions inside.
You can also use
{$IF defined(BLAH)}
or, one of my favourites:
{$IF declared(AnsiString)}
etc...
FWIW, I noticed that the example in the link comes, almost verbatim, from my Console.pas unit.

Why doesn't code from "The Tomes of Delphi" compile?

I'm trying to use the TDRecLst and TDSplyCm units from the code included with The Tomes of Delphi, but I get a compiler error in TDBasics.pas:
I get a similar error in TDStrRes.inc:
What's wrong, and how do I fix it?
The code is available from the author.
You're evidently using a Delphi version that's newer than Delphi 6. Despite being updated in 2005, the code from that book only detects up to that version of Delphi. TDDefine.inc defines a number of compiler symbols based on the version it detects, but when the version you're using isn't anything it recognizes, it defines no symbols. That eventually leads to problems later when the compiler encounters code like this in TDBasics.pas;
implementation
uses
{$IFDEF Delphi1}
WinTypes, WinProcs;
{$ENDIF}
{$IFDEF Delphi2Plus}
Windows;
{$ENDIF}
{$IFDEF Kylix1Plus}
Types, Libc;
{$ENDIF}
{$IFDEF Delphi1}
{$R TDStrRes.r16}
{$ENDIF}
{$IFDEF Delphi2Plus}
{$R TDStrRes.r32}
{$ENDIF}
{$IFDEF Kylix1Plus}
{$R TDStrRes.r32}
{$ENDIF}
const
UnitName = 'TDBasics';
Since none of Delphi1, Delphi2Plus, or Kylix1Plus is defined, the uses clause is empty. When we ignore all the compiler directives and inactive code blocks, the compiler ultimately sees code like this:
implementation
uses
const
UnitName = 'TDBasics';
That's why the compiler complains about expecting an identifier instead of const.
To fix it, you need to teach TDDefine.inc to recognize your version of Delphi. Easier, though, might be to ignore all the version-detection code and hard-code all the symbols that apply to the version you're using. As long as you never use any version older than Delphi 6, all the symbols will apply to all your versions.
Find the following block of code in TDDefine.pas:
{$IFDEF VER140}
{$DEFINE Delphi6}
{$DEFINE Delphi1Plus}
{$DEFINE Delphi2Plus}
{$DEFINE Delphi3Plus}
{$DEFINE Delphi4Plus}
{$DEFINE Delphi5Plus}
{$DEFINE Delphi6Plus}
{$DEFINE HasAssert}
{$ENDIF}
Remove the first and last lines so that the remaining $DEFINE instructions are processed unconditionally.

From which version "vclunit.pas" changed to "Vcl.vclunit.pas"?

I made a component in Delphi 2007 and now I want to make it work with new versions of Delphi so I must change the uses from
uses Controls;
to
uses {$if CompilerVersion > 21}Vcl.Controls{$else}Controls{$ifend};.
But I don't know the compiler version I must write. Anyone knows it?
Unit scope names were introduced in XE2. And XE2 is compiler version 23. So the conditional is:
{$if CompilerVersion >= 23}

How to test using conditional defines if the application is Firemonkey one?

I use DUnit. It has an VCL GUITestRunner and a console TextTestRunner.
In an unit used by both Firemonkey and VCL Forms applications I would like to achieve the following:
If Firemonkey app, if target is OS X, and executing on OS X -> TextTestRunner
If Firemonkey app, if target is 32-bit Windows, executing on Windows -> AllocConsole + TextTestRunner
If VCL app -> GUITestRunner
{$IFDEF MACOS}
TextTestRunner.RunRegisteredTests; // Case 1
{$ELSE}
{$IFDEF MSWINDOWS}
AllocConsole;
{$ENDIF}
{$IFDEF FIREMONKEY_APP} // Case 2 <--------------- HERE
TextTestRunner.RunRegisteredTests;
{$ELSE} // Case 3
GUITestRunner.RunRegisteredTests;
{$IFEND}
{$ENDIF}
Which is the best way to make Case 2 work?
There are no built in conditionals that tell you whether the project's FrameworkType, as specified in the .dproj file, is VCL or FMX. To the very best of my knowledge you cannot switch on that setting in code. Remember also that it is perfectly possible, although certainly not mainstream, to have an application that uses both VCL and FMX. It's really not an either or condition.
So I recommend that you declare your own conditional define that controls whether you use the GUI runner or the text runner.
In fact, you presumably already have some sort of a mechanism to do this. You code names the unit GUITestRunner. So that means it must be in a uses in the same file as the code in the question. How did you conditionally include GUITestRunner in the uses clause?
Note: The same question has been asked on the Embarcadero forums: https://newsgroups.embarcadero.com/message.jspa?messageID=400077
use {$IF Defined(MSWINDOWS)}
instead of {$IFDEF MSWINDOWS}
because {$IFDEF MSWINDOWS} is not working correctly in Firemonkey VCL applications.

Can I generate a custom compiler error? If so, how?

Here is what I want to do. I have a project that must be compiled in some version of Delphi or later. I would like to use a conditional compiler directive to test the Delphi version, and then cause a custom compiler error to be generated with a custom message. Being able to generate a custom compiler warning or hint would also be adaquate if an error is not possible.
Sure, I could put some un-compilable giberish in the conditional code segment, and that's fine. But my question is "Can I generate, conditionally, a custom compiler error?"
Thank you Johan and Serg.
Here is the solution, and more details about the issue. I have an application that was originally built in Delphi 2007. It includes Internet Direct components to attach to a Web service. These use SSL. I recently upgraded my SSL libraries to a later version, and these don't play so well with the Delphi 2007 Indy components. I have now added the following compiler directives to ensure that this application will no longer be compiled with Delphi 2007 or earlier:
{$IF CompilerVersion <= 19.0} // Delphi 2007 = 19.0
{$MESSAGE Error 'This project must be compiled in Delphi 2009 or later'}
{$IFEND}
You can use:
{$Message HINT|WARN|ERROR|FATAL 'text string' }
{$MESSAGE 'Boo!'} emits a hint
{$Message Hint 'Feed the cats'} emits a hint
{$messaGe Warn 'Looks like rain.'} emits a warning
{$Message Error 'Not implemented'} emits an error, continues compiling
{$Message Fatal 'Bang. Yer dead.'} emits an error, terminates compiler
See: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsmessagedirective_xml.html
This works in Delphi 6 and later.
Checking the Delphi version has become easy since CONDITIONALEXPRESSIONS directive was introduced in Delphi 6:
program requires2010;
{$APPTYPE CONSOLE}
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 21.0} // 21.0 is Delphi 2010
{$DEFINE DELPHI2010}
{$IFEND}
{$ENDIF}
begin
{$IFNDEF DELPHI2010}
{$MESSAGE Fatal 'Wrong Delphi Version'}
{$ENDIF}
Writeln('Continued');
Readln;
end.

Resources