在 Visual Studio 中包含头文件

发布于 2024-08-08 19:35:22 字数 299 浏览 8 评论 0原文

假设我有一个包含 3 个项目 XYE 的解决方案。

E 将生成可执行文件,XY 将生成静态库,使得 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬花如无物 2024-08-15 19:35:22

原因如下:

  1. 项目 Y 中的某些函数可能采用 X 中声明的类型的参数(或返回值)。
  2. 如果是这样,编译器可能必须在编译 E 时创建这些参数(或返回值)对象。
  3. 如果是这样的话,E 中绝对需要来自 X 的头文件。

Here's why:

  1. It is possible that some function in project Y takes an argument (or returns a value) which is of a type declared in X.
  2. If so, the compiler may have to create these argument (or return value) objects while compiling E.
  3. If that's the case, header files from X are absolutely needed in E.
不可一世的女人 2024-08-15 19:35:22

如果您构造 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.

迷乱花海 2024-08-15 19:35:22

有时可以重组 C++ 的头文件以使用前向声明来避免您所描述的情况。下面是一个示例:C++ 标头依赖技巧

一个简单的案例:

Xh

class X {
  //...
};

Yh

// #include <X.h> -- remove this
class X; // add forward declaration
class Y {
  X *m_px; // must be a pointer, not a value,
           // otherwise the size of X would need to be known
  //...
};

Y.cpp

#include <X.h> // need to add it here
//...

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

class X {
  //...
};

Y.h

// #include <X.h> -- remove this
class X; // add forward declaration
class Y {
  X *m_px; // must be a pointer, not a value,
           // otherwise the size of X would need to be known
  //...
};

Y.cpp

#include <X.h> // need to add it here
//...
烟雨凡馨 2024-08-15 19:35:22

简短回答:“为什么我必须在 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 编译应该不会出现问题:

#include "Y1.h"

一个好的做法是在 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:

#include "Y1.h"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文