简体   繁体   English

C++ 头文件和源文件 - 包含哪些内容和顺序

[英]C++ Header & Source Files - What To Include And In Which Order

I just started learning C++ and have trouble understanding the concept of header and source files, specifically which ones I'm supposed to include where.我刚开始学习 C++ 并且无法理解头文件和源文件的概念,特别是我应该在哪里包含哪些文件。

Suppose I have the classes A and B, each of whose contents are seperated into a header file and a source file, and I have a file in which I want to use those classes.假设我有 A 类和 B 类,每个类的内容都分为头文件和源文件,并且我有一个要在其中使用这些类的文件。

├ A.h
├ A.cpp
├ B.h
├ B.cpp
└ Main.cpp

Which file do I need to include where, and is it possible to compile/link all files with a single command?我需要在哪里包含哪个文件,是否可以使用单个命令编译/链接所有文件?

Which file do I need to include where我需要在哪里包含哪个文件

#include directive in fact is very simple preprocessor directive, which just adds content of the specified file into the target file. #include指令实际上是一个非常简单的预处理指令,它只是将指定文件的内容添加到目标文件中。 Conventionally you keep declaration of functions in a header file and definition of the said functions in the corresponding cpp file, and thus you at least want to have the header included there (in your case A.cpp includes Ah and B.cpp includes Bh ).通常,您将函数声明保留在头文件中,并将所述函数的定义保留在相应的cpp文件中,因此您至少希望将头包含在其中(在您的情况下, A.cpp包括AhB.cpp包括Bh ) . Additionally you include headers in any file where you use the functions declared in the headers (eg if you use declarations of Ah and Bh in Main.cpp you include those files as well).此外,您在使用标头中声明的函数的任何文件中包含标头(例如,如果您在Main.cpp中使用AhBh的声明,您也包括这些文件)。

PS You, however, can define everything right in the header file, next to declarations, but as I said earlier preprocessor doesn't do anything fancy - it just adds the content of the include in the target file, and you usually don't want to have all definitions in place, because each translation unit which has it included will have the same definitions repeated over and over again. PS 但是,您可以在头文件中的声明旁边定义所有内容,但是正如我之前所说的,预处理器并没有做任何花哨的事情-它只是将包含的内容添加到目标文件中,而您通常不会希望所有定义都到位,因为包含它的每个翻译单元都会一遍又一遍地重复相同的定义。

You include what you need.你包括你需要的东西。

If A.cpp needs Ah then it should include it.如果A.cpp需要Ah那么它应该包含它。 Same for B.cpp probably needing to include Bh . B.cpp同样可能需要包含Bh

If Main.cpp uses A and B then it should include Ah and Bh .如果Main.cpp使用AB那么它应该包括AhBh

Main.cpp should not rely on indirect includes. Main.cpp不应依赖于间接包含。 For example of Ah does include Bh then Main.cpp using both A and B should still include Ah and Bh even if the code would compile if Main.cpp would only directly include Ah (and by that also indirectly including Bh .)例如Ah确实包含Bh然后Main.cpp同时使用AB仍应包含AhBh ,即使如果Main.cpp仅直接包含Ah (并且由此也间接包含Bh )代码将编译。

In Which order?按哪个顺序?

If you follow the rule of always including what you use (rather than relying on indirectly including a header or rely on some other header being included before) then the order of #include directives should not matter.如果您遵循始终包含您使用的内容的规则(而不是依赖间接包含标头或依赖于之前包含的其他标头),那么#include指令的顺序应该无关紧要。 Changing order of includes breaking a build hints on a problem with includes in one of the headers that should be fixed.更改包含的顺序会破坏构建提示,其中包含应修复的标题之一中的包含问题。 If you do things right then order of includes is just a matter of style.如果你做对了,那么包含的顺序只是风格问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM