如何定义版本“和up” ifdefs在Delphi? [英] How to define version "and up" ifdefs in Delphi?

查看:436
本文介绍了如何定义版本“和up” ifdefs在Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在致力于在Delphi XE4中使用Log4D,并且得到一些编译错误,因为它不能在uses子句中找到Contnrs,除非我将它移动到if定义之外。

  {$ IFDEF DELPHI5_UP} 
Contnrs,
{$ ENDIF}

有一点调查发现,ifdef是在一个包含的文件Define.inc中定义的,它有一个阻止每个支持版本的delphi的块,它阻止了几个版本返回:



例如:

  {$ IFDEF VER170} { Delphi 2005} 
{$ DEFINE DELPHI9}
{$ DEFINE DELPHI4_UP}
{$ DEFINE DELPHI5_UP}
{$ DEFINE DELPHI6_UP}
{$ DEFINE DELPHI7_UP}
{$ ENDIF}

{$ IFDEF VER180} {Delphi 2006}
{$ DEFINE DELPHI10}
{$ DEFINE DELPHI4_UP}
{$ DEFINE DELPHI5_UP}
{$ DEFINE DELPHI6_UP}
{$ DEFINE DELPHI7_UP}

所以当它会很容易,以便复制粘贴我fdef for Delphi 2006,并创建一个Delphi XE4块...这似乎是一个不切实际的解决方案。这绝对不是未来的证明...每个新版本你必须去更新这个文件,所以Delphi 4中不存在的一些代码不会使别人的15年以上旧版代码爆炸。



所以我想知道是否有更好的方法来进行条件编译,以便它在编译该行时确实检查是否有Delphi 5或更高版本,而不是需要更新的格式

解决方案

IIRC,Delphi 6引入了条件表达式,这实际上是TLama建议的。要使您的代码使用下面的Delphi版本,您必须检查 {$ IFDEF CONDITIONALEXPRESSIONS} 。如果没有定义,您必须使用旧的 VERxxx 方案来区分Delphi版本。



Delphi 6及更高版本可以使用内置的常量 CompilerVersion RTLVersion 。你使用哪一个取决于你的代码。每当您对 CompilerVersion 使用新的编译器功能测试。对于与 RTLVersion 的RTL或VCL测试相关的任何内容。



总是一个好主意,最新的编译器,只使用条件部分来兼容旧版本。所以代替写入 {$ IF CompilerVersion> = 26.0} {$ IF CompilerVersion < 26.0} 被使用。因此,在将来,更容易删除对旧版本的编译器的支持。


I was working on getting Log4D working in Delphi XE4, and was getting some compile errors because it couldn't find Contnrs in the uses clause, unless I moved it outside the ifdef it was defined in.

{$IFDEF DELPHI5_UP}
  Contnrs,
{$ENDIF}

A little bit of investigating uncovered that the ifdef is defined in an included file Defines.inc which has a block for each "supported" version of delphi which stops a few versions back:

eg:

{$IFDEF VER170}  { Delphi 2005 }
{$DEFINE DELPHI9}
{$DEFINE DELPHI4_UP}
{$DEFINE DELPHI5_UP}
{$DEFINE DELPHI6_UP}
{$DEFINE DELPHI7_UP}
{$ENDIF}

{$IFDEF VER180}  { Delphi 2006 }
{$DEFINE DELPHI10}
{$DEFINE DELPHI4_UP}
{$DEFINE DELPHI5_UP}
{$DEFINE DELPHI6_UP}
{$DEFINE DELPHI7_UP}

So while it would be easy enough to go ahead and copy and paste the ifdef for Delphi 2006 and create a Delphi XE4 block... this seems like an inelegant solution. It's definitely not future proof...every new version you have to go update this file now so some code that wasn't present in Delphi 4 doesn't make someone else's 15+ year old legacy code explode.

So I was wondering if there's a better way to do conditional compilation such that it really does just check whether you have "Delphi 5 or above" when compiling that line, rather than this format that requires updating every single new version of delphi that comes out.

解决方案

IIRC, Delphi 6 introduced conditional expressions, which are actually what TLama suggested. To make your code work with Delphi versions below that, you have to check for {$IFDEF CONDITIONALEXPRESSIONS }. If this is not defined, you have to use the old VERxxx scheme to distinguish between the Delphi versions.

For Delphi 6 and higher you can use the built-in constants CompilerVersion and RTLVersion. Which of these you use depends on your code. Whenever you use a new compiler feature test for CompilerVersion. For anything related to the RTL or VCL test for RTLVersion.

It is always a good idea to code against the newest compiler and only use the conditional part for compatibility to older versions. So instead of writing {$IF CompilerVersion >= 26.0} write your code in a way that {$IF CompilerVersion < 26.0} is used. Thus in the future it is much easier to drop support for older compiler versions.

这篇关于如何定义版本“和up” ifdefs在Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆