Delphi #IF(DEBUG) equivalent?

后端 未结 4 1752
轮回少年
轮回少年 2021-02-05 07:27

Is there a Delphi equivalent of the C# #if(DEBUG) compiler directive?

相关标签:
4条回答
  • 2021-02-05 08:02

    Apart from what lassevk said, you can also use a few other methods of compiler-evaluation (since Delphi 6, I believe) :

    {$IF NOT DECLARED(SOME_SYMBOL)} 
      // Mind you : The NOT above is optional
    {$ELSE}
    {$IFEND}
    

    To check if the compiler has this feature, use :

     {$IFDEF CONDITIONALEXPRESSIONS}
    

    There are several uses for this.

    For example, you could check the version of the RTL; From the Delphi help :

    You can use RTLVersion in $IF expressions to test the runtime library version level independently of the compiler version level.
    Example: {$IF RTLVersion >= 16.2} ... {$IFEND}

    Also, the compiler version itself can be checked, again from the code:

    CompilerVersion is assigned a value by the compiler when the system unit is compiled. It indicates the revision level of the compiler features / language syntax, which may advance independently of the RTLVersion. CompilerVersion can be tested in $IF expressions and should be used instead of testing for the VERxxx conditional define. Always test for greater than or less than a known revision level. It's a bad idea to test for a specific revision level.

    Another thing I do regularly, is define a symbol when it's not defined yet (nice for forward-compatiblity), like this :

     {$IF NOT DECLARED(UTF8String)}
     type
       UTF8String = type AnsiString;
     {$IFEND} 
    

    Hope this helps!

    0 讨论(0)
  • 2021-02-05 08:03

    DebugHook is set if an application is running under the IDE debugger. Not the same as a compiler directive but still pretty useful. For example:

    ReportMemoryLeaksOnShutdown := DebugHook <> 0; // show memory leaks when debugging
    
    0 讨论(0)
  • 2021-02-05 08:13

    These control directives are available:

    {$IFDEF}
    {$ELSE}
    {$ENDIF}
    {$IFNDEF} //if *not* defined
    

    and they can be used as shown here:

    procedure TfrmMain.Button1Click(Sender: TObject);
    begin
      {$IFDEF MY_CONDITIONAL}
      ShowMessage('my conditional IS defined!');
      {$ELSE}
      ShowMessage('my conditional is NOT defined!');
      {$ENDIF}
    
      {$IFNDEF MY_CONDITIONAL}
      ShowMessage('My conditional is explicitly NOT defined');
      {$ENDIF}
    end;
    
    0 讨论(0)
  • 2021-02-05 08:20

    Use this:

    {$IFDEF DEBUG}
    ...
    {$ENDIF}
    
    0 讨论(0)
提交回复
热议问题