[英]Does C++ include all headers a included header file includes?
In this example code, I have 3 files:在此示例代码中,我有 3 个文件:
testHeader.h:测试头.h:
void hello() { }
file1.h:文件1.h:
#include "testHeader.h"
#include <iostream>
void sayHi() { std::cout << "hi" << std::endl; }
file2.h:文件2.h:
#include "file1.h"
void sayHello() { std::cout << "hello" << std::endl; }
If file1.h
includes testHeader.h
and file2.h
includes file1.h
, do testHeader.h
and its functions become accessible in file2.h
?如果file1.h
包含testHeader.h
并且file2.h
包含file1.h
,那么testHeader.h
及其函数是否可以在file2.h
中访问? What about <iostream>
and its functions? <iostream>
及其函数呢?
Unless protected by preprocessor guards in weird ways, yes, you get them all.除非以奇怪的方式受到预处理器守卫的保护,是的,你得到了它们。 #include
is largely a preprocessor trick, roughly equivalent to dumping the text of the include into the source code, and it's transitive; #include
在很大程度上是一种预处理技巧,大致相当于将包含的文本转储到源代码中,并且具有传递性; if you #include <ah>
, you get the expanded form of ah
, including the preprocessor expanded form of all its includes, all their includes, etc., etc. ad infinitum, in a single pass.如果你#include <ah>
,你会得到ah
的扩展形式,包括其所有包含的预处理器扩展形式,所有它们的包含等,等等,无限期地,一次通过。
Note that it's still a good idea to explicitly include all the things you rely on directly;请注意,明确包含您直接依赖的所有内容仍然是一个好主意; sure, the other .h
files might #include <vector>
today, but that's no guarantee the next release will have them if they're not a necessary part of the API being exposed.当然,其他.h
文件今天可能会#include <vector>
,但如果它们不是暴露的 API 的必要部分,则不能保证下一个版本会包含它们。
It's also worth noting that preprocessor include guards (and/or #pragma once
) is used almost universally to mean a .h
file's contents don't get included twice (by convention, not a guarantee; in the case of poorly written .h
files, or weird ones designed for multiple inclusion with different preprocessor setups it won't be obeyed);还值得注意的是,预处理器包含保护(和/或#pragma once
)几乎普遍用于表示.h
文件的内容不会被包含两次(按照惯例,不是保证;在编写不佳的.h
文件的情况下, 或设计用于具有不同预处理器设置的多重包含的奇怪的,它不会被遵守); there is little to no cost to re-including a header in the .cpp
that it already got from a .h
you included.在.cpp
中重新包含 header 几乎没有成本,它已经从您包含的.h
中获得。 On older compilers, without #pragma once
and with no special handling for include guards, it might have to load the header a second time, see the guard, and dump nothing on the second include;在较旧的编译器上,如果没有#pragma once
并且没有对包含保护的特殊处理,它可能必须第二次加载 header,查看保护,并且在第二个包含上不转储任何内容; many newer compilers are smart enough to avoid even that cost.许多较新的编译器足够聪明,甚至可以避免这种成本。 Point is, don't try to optimize by avoiding redundant #include
s;重点是,不要试图通过避免多余的#include
来进行优化; every file should have the complete set of #include
s needed for the things used in that file, no more, no less.每个文件都应该具有该文件中使用的东西所需的完整的#include
集,不多也不少。
If you work on older compilers, without #pragma once, then try to do as following.如果您使用较旧的编译器,没有#pragma once,请尝试执行以下操作。
--- file1.h --- --- 文件 1.h ---
#ifndef FILE1_H
#define FILE1_H
#include "testHeader.h"
#include <iostream>
void sayHi();
#endif
--- file1.cpp --- --- 文件 1.cpp ---
#include "file.h"
void sayHi() { std::cout << "hi" << std::endl; }
--- file2.h --- --- 文件 2.h ---
#ifndef FILE2_H
#define FILE2_H
#include "file1.h"
#include <iostream>
void sayHello();
#endif
You shouldn't make a body of function in HEADER file.您不应该在 HEADER 文件中制作 function 的正文。 It might cause a compile error for multiple links for the same function.对于同一个 function 的多个链接,它可能会导致编译错误。 Please write function' prototype and body into Header and Source file seperately.请将函数原型和函数体分别写入 Header 和 Source 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.