为什么{$IFDEF MSWINDOWS}在Delphi XE5中被{$IF定义(MSWINDOWS)}替换)

前端之家收集整理的这篇文章主要介绍了为什么{$IFDEF MSWINDOWS}在Delphi XE5中被{$IF定义(MSWINDOWS)}替换)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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/XE6/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,9
  {$ENDIF}
{$ENDIF}

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

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

我猜Embarcadero决定清理代码库.

原文链接:https://www.f2er.com/delphi/101562.html

猜你在找的Delphi相关文章