Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

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}
like image 361
Cary Jensen Avatar asked May 28 '11 15:05

Cary Jensen


2 Answers

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.

like image 153
Johan Avatar answered Sep 22 '22 23:09

Johan


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.
like image 6
kludg Avatar answered Sep 21 '22 23:09

kludg