Delphi #IF(DEBUG)等效吗?
时间:2020-03-06 14:51:46 来源:igfitidea点击:
是否有等效于C#if(DEBUG)编译器指令的Delphi?
解决方案
用这个:
{$IFDEF DEBUG} ... {$ENDIF}
除了lassevk所说的以外,我们还可以使用其他一些编译器评估方法(自Delphi 6起,我相信):
{$IF NOT DECLARED(SOME_SYMBOL)} // Mind you : The NOT above is optional {$ELSE} {$IFEND}
要检查编译器是否具有此功能,请使用:
{$IFDEF CONDITIONALEXPRESSIONS}
有几种用途。
例如,我们可以检查RTL的版本。从Delphi帮助中:
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}
同样,可以再次从代码中检查编译器版本本身:
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.
我经常做的另一件事是在尚未定义符号时进行定义(前向兼容性很好),如下所示:
{$IF NOT DECLARED(UTF8String)} type UTF8String = type AnsiString; {$IFEND}
希望这可以帮助!
这些控制指令可用:
{$IFDEF} {$ELSE} {$ENDIF} {$IFNDEF} //if *not* defined
它们可以按如下所示使用:
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;
如果应用程序在IDE调试器下运行,则会设置DebugHook。与编译器指令不同,但仍然非常有用。例如:
ReportMemoryLeaksOnShutdown := DebugHook <> 0; // show memory leaks when debugging