Search code examples
delphidelphi-xe7delphi-xe8uses-clause

Stop Delphi automatically adding units incorrectly


I am updating our projects from XE7 to XE8. For the time being they will still need to work with XE7, so a few conditionals are required. For example, ImageList has changed units, so Vcl.ImgList is Syste.ImageList in XE8. To make it work on both Delphi versions the uses clause looks something like this:

uses
  System.SysUtils, System.Classes, Vcl.Controls
  {$IF CompilerVersion >= 29.0}
  ,System.ImageList
  {$else}
  ,Vcl.ImgList
  {$endif}

  ,cxGraphics;

Sometimes this works fine. However, quite often Delphi automatically re-adds the System.Imagelist unit even though it is already there, albeit in a conditional e.g.

uses
  System.SysUtils, System.Classes, Vcl.Controls
  {$IF CompilerVersion >= 29.0}
  ,System.ImageList
  {$else}
  ,Vcl.ImgList
  {$endif}

  ,cxGraphics, System.ImageList;

When this is compiled, XE8 complains.

[dcc32 Error] dmImagesU.pas(13): E2004 Identifier redeclared: 'System.ImageList'

a) Why does Delphi add the unit? b) Anybody know a workaround?


Solution

  • The "parser" that is responsible for adding units to the uses in this case is not considering compiler directives properly (that issue already occured with the introduction of System.Actions.pas).

    I usually create a dummy unit for older Delphi versions so I don't have to put any directives into the uses.

    So in your case just create an empty System.ImageList.pas and put that somewhere <= XE7 finds it.