Stop the IDE from adding uses units automatically

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?


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.


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.

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

上一篇: 单位稍有变化,然后所有的dcu文件重新编译

下一篇: 停止IDE自动添加使用单位