Delphi #IF(DEBUG) 等效吗?

发布于 2024-07-06 12:03:04 字数 44 浏览 8 评论 0原文

Delphi 是否有与 C# #if(DEBUG) 编译器指令等效的代码?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凤舞天涯 2024-07-13 12:03:04

用这个:

{$IFDEF DEBUG}
...
{$ENDIF}

Use this:

{$IFDEF DEBUG}
...
{$ENDIF}
中二柚 2024-07-13 12:03:04

如果应用程序在 IDE 调试器下运行,则设置 DebugHook。 与编译器指令不同,但仍然非常有用。 例如:

ReportMemoryLeaksOnShutdown := DebugHook <> 0; // show memory leaks when debugging

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
坚持沉默 2024-07-13 12:03:04

除了 lassevk 所说的之外,您还可以使用其他一些编译器评估方法(我相信从 Delphi 6 开始):

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

要检查编译器是否具有此功能,请使用 :

 {$IFDEF CONDITIONALEXPRESSIONS}

这有多种用途。

例如,您可以检查 RTL 的版本; 来自德尔福帮助:

您可以在 $IF 中使用 RTLVersion
测试运行时的表达式
库版本级别独立
编译器版本级别。
示例:{$IF RTLVersion >= 16.2} ...
{$IFEND}

此外,还可以从代码中检查编译器版本本身:

CompilerVersion 的值由
当系统单位是编译器
编译。 表示修改
编译器功能级别 /
语言语法,可能会进步
独立于 RTL 版本。
CompilerVersion可以在$IF中测试
表达式和应该使用
而不是测试 VERxxx
条件定义。 始终测试
大于或小于已知
修订级别。 这是一个坏主意
测试特定修订级别。

我经常做的另一件事是在尚未定义符号时定义它(有利于向前兼容性),如下所示:

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

希望这有帮助!

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!

同展鸳鸯锦 2024-07-13 12:03:04

这些控制指令可用:

{$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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文