30

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}
Cary Jensen
  • 3,751
  • 3
  • 32
  • 55

2 Answers2

46

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.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • 1
    Indeed. And of course you can put these between `{$IFDEF}` statements like Cary wants to do to warn about the compiler version required. – Marjan Venema May 28 '11 at 17:32
  • Not that I'm not happy with the upvotes, but what's so great about this answer? Does everybody have a sudden urge to create custom compiler messages? – Johan May 29 '11 at 21:55
  • 2
    I think it's because it's not something that comes up often, but can be really useful. Also, I poked around a bit before posting this question and didn't see anything off hand. As a result, it's probably a little known technique. I certainly didn't know about it myself, and I've written about and used every version of Delphi. – Cary Jensen May 29 '11 at 22:46
6

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.
Johan
  • 74,508
  • 24
  • 191
  • 319
kludg
  • 27,213
  • 5
  • 67
  • 118
  • in which version was this introduced? – Johan May 28 '11 at 16:08
  • @Johan: I'm sure it was introduced before Delphi 2007. Currently I have no legacy Delphi versions to check. – kludg May 28 '11 at 16:21
  • You can check against IFDEF VER140 (Delphi6) and alike defs, VER22 = Delphi XE. – Kromster May 28 '11 at 16:41
  • 2
    I think conditionalexpressions was introduced in Kylix on the Linux platform and Delphi 6 on the Windows platform. Yup it is: http://books.google.com/books?id=V9t0lA-BPUAC&pg=PA52&lpg=PA52&dq=%22delphi+6%22+CONDITIONALEXPRESSIONS&source=bl&ots=4-UBsNal5r&sig=e-8cfyKVxOe5Z_wDZ1jtqKCyycw&hl=en&ei=s0ThTf3YLI-cOsLenNUG&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBcQ6AEwAA#v=onepage&q=%22delphi%206%22%20CONDITIONALEXPRESSIONS&f=false – Jeroen Wiert Pluimers May 28 '11 at 18:54