开发者

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

开发者 https://www.devze.com 2023-03-08 19:19 出处:网络
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

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开发者_StackOverflow中文版 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消