在 Visual Studio 中包含头文件
假设我有一个包含 3 个项目 X
、Y
和 E
的解决方案。
E
将生成可执行文件,X
和 Y
将生成静态库,使得 Y
包含 < code>X和E
包含Y
的头文件。
现在,我的问题是:为什么我必须在E
中包含X
的头文件目录?
Suppose I have a solution with 3 projects X
,Y
, and E
.
E
will generate an executable and X
and Y
will generate static libraries such that Y
includes the header files of X
and E
includes the header files of Y
.
Now, my question is: why do I have to include the directory of the header files of X
in E
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
原因如下:
Y
中的某些函数可能采用X
中声明的类型的参数(或返回值)。E
时创建这些参数(或返回值)对象。E
中绝对需要来自X
的头文件。Here's why:
Y
takes an argument (or returns a value) which is of a type declared inX
.E
.X
are absolutely needed inE
.如果您构造 Y 使其对 X 的依赖关系完全封装,则可以避免这种情况。这可能可行,也可能不可能,具体取决于 X 和 Y 的具体情况。但是,如果 Y 向 E 呈现的接口不需要包含 X 的任何详细信息,那么 E 项目甚至不需要间接包含来自 X 的标头。在这种情况下,只有 Y 的实现文件(.c 或 .cpp 文件)将包含来自 X 的标头。在 Y 标头中对 X 中的类型使用前向声明可以帮助实现 X 在 Y 中的封装
。是一个值得实现的好目标,但它可能并不总是可能实现,即使有可能,它也可能比您(或您的管理层)想要付出的努力更多。
You can avoid this situation if you construct Y such that its dependencies on X are completely encapsulated. This may or may not be possible depending on the specifics of X and Y. But if the interface the Y presents to E doesn't need to have any details of X in it, then the E project shouldn't need to even indirectly include headers from X. In thins case only the implementation files of Y (the .c or .cpp files) would include headers from X. Using forward declarations for types in X in the Y headers could help achieve this encapsulation of X in Y.
This is a good goal to reach for, but it might not always be possible and even when it is possible it might be more effort than you (or your management) might want to put forth.
有时可以重组 C++ 的头文件以使用前向声明来避免您所描述的情况。下面是一个示例:C++ 标头依赖技巧。
一个简单的案例:
Xh
Yh
Y.cpp
It is sometimes possible to restructure header files for C++ to use forward declarations to avoid the situation that you describe. Here is an example: C++ header dependency tricks.
A simple case:
X.h
Y.h
Y.cpp
简短回答:“为什么我必须在 E 中包含 X 的头文件目录?”...你不应该。 Y 的客户端不必知道 Y 依赖于 X。
长答案: 仅当 Y 的接口(签名)使用在 E 中声明的内容时,才需要在 E 中包含 X 的标头但是,如果 Y 的标头是“正确构造的”,那么它们将在 Y 标头本身中包含 X 的标头,并且您不必在 E 中显式包含 X 标头(包括 Y 标头会自动包括 X 标头)。
通过“正确构造”,我的意思是,如果 Y 中的 Y1.h 中的签名依赖于(比如说)X3.h 和 X7.h,那么 Y1.h 应该包含这些文件(直接或间接)。这样,Y1.h 的任何客户端都不必知道它的依赖项是什么,并且必须单独包含这些依赖项。作为一个简单的测试,包含以下行的 .cpp 编译应该不会出现问题:
一个好的做法是在 Y1.cpp 中包含任何其他文件之前 #include "Y1.h"。如果 Y1.h 缺少依赖项,编译器会通知您。
Short answer: "Why do I have to include the directory of the header files of X in E?"... You shouldn't. Client's of Y should not have to know that Y depends on X.
Long answer: You need to include the header of X in E only if the interface (signatures) of Y makes use of things declared in the header of X. But if the header of Y were "properly constructed", then they would include the headers of X in the Y header itself and you wouldn't have to include the X header in E explicitly (including the Y header would automatically include the X header).
By "Properly constructed" I meant that if the signatures in Y1.h in Y depend on (say) X3.h and X7.h, then Y1.h should include those files (directly of indirectly). This way, any client of Y1.h would not have to know what it's dependencies are and have to include those dependencies separately as a result. As a simple test, a .cpp consisting of the following line should compile without issues:
A good practice is to #include "Y1.h" before including any other file in Y1.cpp. If Y1.h is missing dependencies, the compiler will let you know.