delphi - 为什么在Delphi XE5中{$IFDEF MSWINDOWS}被替换为{$IF Defined(MSWINDOWS)}?

标签 delphi conditional-statements delphi-xe5 conditional-compilation defined

在XE5中所有条件编译如

{$IFDEF MSWINDOWS} 

替换为

{$IF defined(MSWINDOWS)}

例如XE4中的System.Diagnostics.pas有

...
implementation

{$IFDEF MSWINDOWS}
uses Winapi.Windows;
{$ENDIF}
{$IFDEF MACOS}
uses Macapi.Mach;
{$ENDIF}

{ TStopwatch }
...

现在在 XE5 中它看起来像:

...
implementation
{$IF defined(MSWINDOWS)}
uses Winapi.Windows;
{$ELSEIF defined(MACOS)}
uses Macapi.Mach;
{$ELSEIF defined(POSIX)}
uses Posix.Time;
{$ENDIF}

{ TStopwatch }
...

是否有任何特殊原因我也应该迁移类似的调用?

最佳答案

根据 Delphi 文档:

http://docwiki.embarcadero.com/RADStudio/Rio/en/Conditional_compilation_%28Delphi%29

The conditional directives {$IFDEF}, {$IFNDEF}, {$IF}, {$ELSEIF}, {$ELSE}, {$ENDIF}, and {$IFEND} allow you to compile or suppress code based on the status of a conditional symbol.

{$IFDEF}{$IFNDEF} 只允许您使用之前由 {$DEFINE ...} 设置的定义>.
然而 {$IF ..} 指令更加灵活,因为:

Delphi identifiers cannot be referenced in any conditional directives other than {$IF} and {$ELSEIF}.

const LibVersion = 6;  //One constant to define the libversion.
{$IF LibVersion >= 10.0}
  do stuff that covers LibVersion 10,11 and 12
{$ELSEIF Libversion > 5.0}
  do other stuff that covers LibVersion 6,7,8,9
{$IFEND}

如果你尝试使用定义来做到这一点,你就必须这样做

{$DEFINE Lib1}
{$DEFINE Lib2}
{$DEFINE Lib3}
{$DEFINE Lib4}
{$DEFINE Lib5}
{$DEFINE Lib6} //all previous versions have to be defined.

{$IFDEF Lib10}
  do stuff that covers LibVersion 10, 11 and 12
{$ELSE}
  {$IFDEF Lib6}
    do stuff that covers LibVersion 6,7,8,9
  {$ENDIF}
{$ENDIF}

这只是处理定义的稍微高级的版本。
{$IF ..} 表示法更强大,它允许您查询常量表达式而不仅仅是定义。

{$IF ..} 指令是在 Delphi 6 中引入的。

我猜 Embarcadero 决定清理代码库。

关于delphi - 为什么在Delphi XE5中{$IFDEF MSWINDOWS}被替换为{$IF Defined(MSWINDOWS)}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597548/

相关文章:

delphi - 列出现有对象以读取和更改其属性的理想方法是什么?

delphi - 从 Delphi 代码调用 Visual C++ DLL 的函数

c++ - "if(T t = ...) { } else return t;"的优雅方式?

delphi - 为什么滚动 TListView 需要整个表单重新绘制 - Delphi Firemonkey

delphi - 如何向 FireMonkey 工具栏添加按钮?

delphi - Delphi的可拖动选项卡控件?

sas - 如何在 SAS 中将条件作为宏参数传递

c++ - 将逗号运算符与条件运算符一起使用

delphi - RichEdit 控件在成为其他控件的父控件时停止绘制文本

delphi - 如何在jenkins节点上设置多个delphi版本?