C++ 不包括来自先前 header 的包含

标签 c++ include visual-studio-2015

假设我有以下内容:

Main.cpp

#include <Windows.h>
#include "B.h"

...

-

B.h

...
SomePrototypeFunctionNeedingWindowsH();

-

在 B.h 中,我不需要再次包含 Windows.h,因为它已经事先包含在内。为清楚起见,我希望需要为每个需要它的新文件包含 Windows.h。我正在使用 VS2015。

这可以做到吗?

这可以在不影响编译时间的情况下完成吗?

这会被认为是可接受的做法吗?

如果这样做,我会遇到任何问题吗?

最佳答案

也许您正在寻找 C++ 中的预处理器指令。 它们是这样的:

#ifndef HEADERFILE_H
#define HEADERFILE_H
/*Your header declarations/definitions*/
#endif

在这种预处理器技术中,您基本上是告诉编译器它不应该多次包含相同的 header 。 Refer to this post for more thorough understanding

关于C++ 不包括来自先前 header 的包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387933/

相关文章:

c++ - C++ 函数中动态分配大结构时的返回值选择

c++ - 访问父类(super class)类型成员对象的 protected 成员——一个优雅的解决方案

c++ - 调用 void 函数时 cout 没有输出

c - C语言中如何链接两个文件

c++ - Windows 上的 std::conditional_variable::wait_for、std::thread::sleep_for 受调整时钟的影响

dll - 如果我仍然需要 .dll 和 .pri 文件,那么使用 .winmd 库有什么意义?

C++多线程程序试图使用删除的函数

php - 我怎样才能让 PHP INCLUDE 工作?

html - 如何在不将 Html 更改为 sHtml 的情况下包含其他代码(菜单栏或侧边栏)

c++ - 错误 LNK2019 无法解析的外部符号 Tesseract OCR C++ 使用 VS 2015