Stop the IDE from adding uses units automatically

一曲冷凌霜 提交于 2020-01-13 14:40:17

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/33676195/stop-the-ide-from-adding-uses-units-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!