How to conditionally include some units
Asked Answered
A

1

8

I have various define statements for handling different Delphi versions in an include file. This include file is "included" in an unit. The compiler respects the defines given in the include file but the IDE not. This results in an addition of certain units to the uses clause which are can be already there - enclosed in a DEFINE compiler directive.

Therefore, if a unit is added which isn't available in pre Delphi XE3 you will have a big problem because if you let the IDE add the unit and can not compile it with a pre Delphi XE3 version which doesn't have that unit.

E.g. a project with one unit with an TActionList on it.

  1. MYINCLUDE.INC only one define {$DEFINE DELPHIXE3}

  2. A sample unit may look like

    unit Unit1;
    
    {$I MYINCLUDE.INC}                
    
    uses
       Winapi.Windows, 
       Winapi.Messages, 
       System.SysUtils, 
       System.Variants, 
       System.Classes, 
       Vcl.Graphics,
       Vcl.Controls, 
       Vcl.Forms, 
       Vcl.Dialogs,
       {$IFDEF DELPHIXE3} System.Actions, {$ENDIF}
       Vcl.ActnList;
      ...
    
  3. After a save the IDE adds System.Actions add the end of the uses list which in turn results in an Identifier redeclared compiler error. If you delete it the IDE will add it again the next save.

I have just reported that to QC #111178.

Is there a workaround for that bug?

Christian

Aplacental answered 8/12, 2012 at 9:24 Comment(6)
IDE has always been a bit lousy parsing conditionals. I expect your QC report to be closed as designed. The issue has been raised again and again with always the same response.Acting
IDE always mangled conditionals in *.dpk files but this issue is something new and disappointing.Yate
@Serg It always behaved this way in .pas files too. Nothing new here. Been like this since forever.Acting
I would not bother too much, it applies to Delphi's built-in units, which you will often use elsewhere in your program. Just include them 'always'. For your own units Delphi won't do this.Hydromel
@JanDoggen System.Actions doesn't exist in XE2.Acting
@David Ah yes, forgot the OP's 'different versions' after coming to the end of the answers/comments. I should've done what I learned at school: reread the question to see if I answered the actual question or something else ;-)Hydromel
A
4

Probably the easiest thing to do is to use the unit alias feature to help. In order for this to work you need different project settings for different compiler versions. For example, different .dpr and .dproj files for each supported compiler version.

In your XE2 project you define a unit alias like so:

System.Actions=Vcl.ActnList

In the XE3 project you omit that alias.

Then in your .pas file you can happily use System.Actions with no problems in either version of Delphi.

An even simpler solution is to create an empty unit named System.Actions that you only include in your project for XE2 builds.

Acting answered 8/12, 2012 at 9:38 Comment(1)
Yes, that sounds as the easiest/cleanest workaround for that problem. Thank you.Aplacental

© 2022 - 2024 — McMap. All rights reserved.