2

One feature I miss in Delphi (I hope it is at all possible) is that I cannot have Units automatically include their dependent units. This is possible in c++ headers. For example, in c++:

dependentHeader.h:

#include "baseHeader.h"

Any headers included in baseHeader.h are available in dependentHeader.h. Another example is the precompiled header, whatever I include in the precompiled header is available to all header files in the project. This is very useful for including frequently used headers throughout a project.

Now back to Delphi: I have a Unit called DebugService In order to use it other units are required: DependentUnit1, DependentUnit2.

So in every Unit I use DebugService I have to manually add all the other dependent units: DependentUnit1, DependentUnit2.

What I want is just to be able to specify DebugService as a dependency and have all its dependencies come along?

So, in other words I want:

uses
  DebugService;

and NOT:

uses
  DebugService, DependentUnit1, DependentUnit2;

Is this at all possible?

Thank you!

4

4 回答 4

12

具有讽刺意味的是,当一个更好的问题是“为什么 C++ 在 2013 年还没有模块”时,你会问这个问题。

Delphi 的编译单元通常不会分成重复的 .h 和 .cpp 文件。您可能已经注意到 Delphi 单元有一个接口和实现部分。这反过来成为一个真正的模块系统,编译的 .DCU 文件与 C++/C 编译器“.obj”文件有很大不同,因为当遇到“使用 UnitX”时,编译器可以非常快速地读取接口区域。

最近,Apple 的 CLANG/LLVM 编译器开发人员开始向最新的 CLANG/LLVM C 和 Objective-C 编译器添加真正模块支持的基础。这意味着 XCode 中对预编译头文件的支持不再是首选的处理方式,因为真正的模块比预编译头文件更好。你可以说一个预编译的头文件系统就像有一个模块,只有一个模块,当你不能拥有真正的东西时,你很高兴拥有一个破旧的杂物,这就是所谓的模块。你可能会说,你是一个windows开发者,你关心CLANG/LLVM什么?只是这证明世界正在慢慢放弃预编译,并最终转向模块。C++ 标准化委员会,

简而言之,我们可能会说您的问题可能会问,如果无马马车将获得允许它加速缓存和快速部署燕麦到马动力装置的功能。

我们这里不需要那个。我们有一个真正的编译器,支持真正的模块。故事结局。您可能会注意到模块(在 clang/llvm 中)比预编译的头文件要快。它们也不是问题的根源,而预编译的头文件几乎是无穷无尽的疯狂问题的根源。

于 2013-06-24T22:19:02.753 回答
6

预编译的标头没有任何与标准标头不同的语义含义。它们只是为了缩短编译时间而进行的优化。通常 Delphi 编译比 C++ 编译器快得多,因此不需要优化。

您不能使用单元 A 并传递使用单元 A 的所有依赖项。如果你想使用一个单元的定义,它必须在uses 子句中列出。

于 2013-06-24T22:09:58.213 回答
5

Delphi 中没有与预编译头文件等效的东西。如果在其自己的部分的声明中使用声明,则uses需要添加额外的引用,并且其声明随后被其他单元使用,因此它们依赖于那些其他单元。如果你可以设计你的单元来减少接口依赖,只在节中使用依赖单元,那么你就不必再在其他单元的子句中包含and了。但我明白这并不总是可能的。DebugServiceDependantUnit1DependentUnit2interfaceimplementationDependantUnit1DependantUnit2uses

如果您需要在多个单元之间共享代码,最好将该代码移动到其自己的单元/包中。

于 2013-06-24T22:12:42.820 回答
1
 #include "baseHeader.h"

相当于

 {$I baseHeader.pas}

您可以将任何您喜欢的内容放入该文件中。甚至整个界面部分。

您的问题的另一种选择是使用条件定义。

在主项目文件中

{$DEFINE debugMyApp} 

在您使用的每个单元中

use 
  abc 
{$IFDEF debugMyApp}
   , additionalUNit1
   , additionalUNit2 
   , etc
{$ENDIF}
   ;
于 2013-06-25T06:40:33.543 回答