3

I am moving a Lazarus project to Delphi Seattle.

The Lazarus project depends on 40+ units (including controls) and has several applications.

In the uses clause of all the projects they used the following:

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBCtrls, {$I OurLibrary.inc};    

where they included those 40+ units with $I OurLibrary.inc.

As some of those units are controls i registered them in Delphi.

However if i save the project or build / compile it, Delphi adds the units in the uses part again.

  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBCtrls, uOurEdit, {$I OurLibrary.inc}; 

In this case the unit uOurEdit got added again even tho it is in $I OurLibrary.inc.

If i manually delete it and compile the project again it runs. Once i switch back in designer mode and try to run it the same thing keeps happening - it adds uOurEdit again.

Once you remove a unit Lazarus doesn't add it again. Delphi does that.

Is there are way to tell Delphi to stop readding units or stop add units automatically at all?

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
Tommy
  • 596
  • 6
  • 30
  • See also [Why is my component auto adding other units to the uses interface?](http://stackoverflow.com/q/14660311/757830). – NGLN Nov 12 '15 at 19:16

2 Answers2

11

There are certain parts of your code that the IDE considers to be under its control. This includes much of the DPR file, the default published section of a form or data-module declaration, and the uses clause of the unit's interface section. It's best not to fight the IDE on this. You'll eventually lose.

I wouldn't recommend using an include directive in a uses clause. As you've already noticed, the IDE doesn't read the included file to determine the unit list. The Form Designer automatically adds units it thinks it needs, and there's no way to stop that.

Since the IDE will automatically add controls' units when they're used, you should be able to safely remove them from your include file anyway.

You might also consider moving your list of units to the uses clause in the implementation section. The IDE doesn't touch that one.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • The OP mentions that some of the units contain controls which presumably are often placed on the forms and so the units **have** to be present in the *interface* uses list. Otherwise the controls will have to be instantiated at runtime with inconveniently generalised super-class references in the form class. – Deltics Nov 12 '15 at 20:15
  • See paragraph 3, @Deltics, wherein I suggest removing those units from the include file because the IDE will add them to the `interface` section automatically. – Rob Kennedy Nov 12 '15 at 20:19
  • I understand the IDE not following the `include` directive, but it would be nice if it considered your build configuration's unit aliases when deciding which units are missing so that it doesn't add duplicates... – ardnew Dec 16 '15 at 18:56
2

I agree that using an include file is not a good idea but there is actually a way to stop the IDE adding those units automatically.

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBCtrl,
  {$IFDEF DUMMY}
  // add those units here which you don't want to automatically add
  // the IDE won't add them but they won't be part of the uses assuming
  // DUMMY is undefined
  uOurEdit,
  {$ENDIF}
  // Here comes the rest of the units
  {$I OurLibrary.inc};

This might not work for you since you have to manually add uOurEdit anyway so maybe it is better to follow Rob's advice. However this technique can be used to stop the IDE adding units automatically.

NGLN
  • 43,011
  • 8
  • 105
  • 200
RM.
  • 1,984
  • 19
  • 29