Delphi条件汇编在uses子句中 [英] Delphi conditional compilation in uses clause

查看:213
本文介绍了Delphi条件汇编在uses子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改我的Delphi 2010代码,以编译XE7(并希望保留在2010年编译的能力)。所以在安装我的主窗体的单元中,我添加了条件指令。以下工作在2010年正常

I am trying to modify my Delphi 2010 code to compile in XE7 (and want to retain the ability to compile it in 2010). So in the unit that houses my mainform I added conditional directives. The following works fine in 2010

uses 
  {$IF CompilerVersion >= 24}System.Actions, {$ELSE}Actnlist,{$IFEND}
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,  Dialogs;

但XE7会自动添加一个 System.Actions 最后创建一个using子句,现在已经声明了System.Actions两次(见下文),并给出了错误消息 [dcc32错误] MyForm.pas(10):E2004标识符重新声明:'系统。操作。为什么XE7不接受条件指令内的单位?

But XE7 automatically adds a System.Actions at the end to create a uses clause that now has System.Actions declared twice (see below), and gives an error message [dcc32 Error] MyForm.pas(10): E2004 Identifier redeclared: 'System.Actions'. Why is XE7 not accepting the unit from within the conditional directive ?

uses 
  {$IF CompilerVersion >= 24}System.Actions, {$ELSE}Actnlist,{$IFEND}
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,  Dialogs,
  System.Actions; // <- automatically added


推荐答案

界面使用子句将被IDE修改,并且实现这个过程的程序稍微复杂一些(如您所发现的那样)。同样的问题影响到项目使用条款。不幸的是,在Form / DataModule使用条款的情况下,这是很难避免的。

As Ken says, the interface uses clause will be modified by the IDE and the processes by which this is achieved are somewhat less than sophisticated (as you have discovered). The same problem affects the project uses clause. Unfortunately this is much harder to avoid in the case of Form/DataModule uses clauses.

您可以使用单位别名(请参阅David Heffernan的答案),但需要注意的是,如果为IDE希望添加的单元创建一个别名,则IDE仍将添加对所需单元的引用,因为它不会将该别名识别为所需单元。对系统单位的混淆将避免这一点,因为它已被每个单元(隐含地)使用。

You could use a Unit Alias (see David Heffernan's answer) but need to be aware that if you create an alias for a unit that the IDE wishes to add, then the IDE will still add a reference to the required unit since it does not recognise the alias as identifying that required unit. Aliasing to the System unit will avoid this since it is already (implicitly) used by every unit.

另一个替代方法是删除所有这样的条件从您的使用列表中,根据需要创建place-holder单元,以便您希望在项目中使用的不同编译器可以由每个IDE坚持要求的列表组合使用的单用途列表来满足(IDE不会从使用列表中删除未使用的单元,这通常是一个投诉,但在这种情况下实际上有助于解决您的问题)。

Another alternative is to remove all such conditionals from your uses list and instead create place-holder units as required so that the different compilers you wish to use on the project can each be satisfied by the single uses list combined from the list that each IDE insists is required (the IDE won't remove unused units from the uses list, something that is often a complaint but in this case actually helps solve your problem).

在这种情况下,在您的Delphi 2010项目中创建一个空的动作单位:

In this case, in your Delphi 2010 project create an empty Actions unit:

 unit Actions;
 interface
 implementation
 end.

您当然需要确保本机在项目的XE7版本的项目路径。

You will of course need to ensure that this unit is not in the project path for your XE7 version of the project.

实现此目的的一种方法是确保空的 Actions.pas 单元不明确在DPR使用列表中列出,但被放置在您的项目源的子文件夹(例如占位符)中。然后,您可以将此子文件夹添加到Delphi 2010版本的项目搜索路径中,但不能将XE7版本添加到

One way to achieve that would be ensure that the empty Actions.pas unit is not explicitly listed in the DPR uses list, but is placed in a subfolder of your project source (e.g. 'placeholders'). You can then add this subfolder to the project search path for the Delphi 2010 version but not the XE7 version:

 \Project Folder

     project2010.dpr
     project2010.dproj
     projectXE7.dpr
     projectXE7.dproj

     \placeholders
          Actions.pas

如果您发现每个不同版本都需要占位符,那么您将需要单独的占位符文件夹。您可以创建更多版本的子文件夹,例如:

If you find that you need placeholders for each of the different versions then you will need separate placeholder folders. You might create further version specific subfolders, for example:

     \placeholders
          \2010
               Actions.pas
          \XE7
               D2010UnitNotPresentInXE7.pas

这种结构可能建议您从创建自动/自动记录组织的角度来看是可取的。

This sort of structure might be advisable simply from the point of view of creating an auto/self documenting organisation.

请注意,这仅适用于处理使用框架等)的界面部分的/ strong>子句。在非可视化单元或实施部分中,IDE不会干扰,因此条件编译指令应不会出现问题。

Note that this is only required for dealing with unit references in the uses clause of the interface section of Forms (or Frames etc). In non-visual units or in the implementation section, the IDE does not interfere so conditional compilation directives should present no issues there.

这篇关于Delphi条件汇编在uses子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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