How to conditionally include some units

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.

Eg a project with one unit with an TActionList on it.

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

  • 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;
      ...
    
  • 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


    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.

    链接地址: http://www.djcxy.com/p/11588.html

    上一篇: IMG VS TEXT?

    下一篇: 如何有条件地包括一些单位