简体   繁体   English

c ++何时包含cpp,即使我们有.h文件

[英]c++ when to include cpp even if we have .h file

I am reading the book Absolute C++ 5th edition. 我正在读“绝对C ++第五版”一书。 In page 716, I don't really understand why it needs include "pfarray.cpp" 在页716中,我真的不明白它为什么需要包含“pfarray.cpp”

Is include "pfarray.h" not enough? 是否包含“pfarray.h”还不够?

More specifically, even if we have declarations in .h file but implementations in .cpp files, when we still have to include .cpp file? 更具体地说,即使我们在.h文件中有声明但在.cpp文件中有实现,当我们仍然必须包含.cpp文件时?

Thank you in advance. 先感谢您。

When you write #include anything.any_extension , the extension doesn't really matter to the preprocessor. 当你写#include anything.any_extension ,扩展对预处理器来说并不重要。 It's really like a "take the contents from that file and paste it into this file" kind of brute mechanism. 它实际上就像是“从该文件中获取内容并将其粘贴到此文件中”这种粗暴机制。 So you can include anything without error provided that the code inside is legit, and you can name a header file with any extension. 因此,如果内部代码是合法的,您可以包含任何没有错误的内容,并且您可以使用任何扩展名来命名头文件。 So you can even name them with a .txt extension and it wouldn't really matter to the preprocessor. 所以你甚至可以使用.txt扩展名来命名它们对预处理器来说并不重要。

I would suggest that the practice of including source files is rather confusing, mainly from a build standpoint since it's not very clear whether a source file (cpp, cc, etc) is supposed to be built as a separate object file to link against or #included or both. 我建议包含源文件的做法相当混乱,主要是从构建的角度来看,因为不清楚源文件(cpp,cc等)是否应该构建为链接的单独对象文件或#包括或两者。

Yet it's sometimes done anyways. 然而,它有时会完成。 For example, pfarray.cpp might contain an implementation for a template since templates typically need their full implementation visible at compile time at the site generating the code, and sometimes authors establish a habit of #including files with source file extensions to avoid putting the implementation details into the same header file while uniformly conforming to a style that favors putting all such details into files named with a source file convention. 例如, pfarray.cpp可能包含模板的实现,因为模板通常需要在生成代码的站点的编译时可见其完整实现,有时作者建立#including包含源文件扩展名的文件的习惯,以避免执行将详细信息放入同一个头文件中,同时统一符合有利于将所有此类详细信息放入以源文件约定命名的文件的样式。

Another reason this can be done, but I don't think it's the reason it was done in your case, is as a build optimization (see Unity builds). 这可以做到的另一个原因,但我不认为这是你的情况下完成的原因,是作为构建优化(参见Unity构建)。 It can sometimes be more efficient to compile and link fewer files, so using #include for source files can be a crude way to fuse them all together into a single build target. 编译和链接较少的文件有时会更有效,因此将#include用于源文件可能是将它们全部融合到一个构建目标中的粗略方法。

You don't have to. 你不必。

A translation unit is a group of files with definitions and declarations. 翻译单元是一组包含定义和声明的文件。 Compiling a translation unit, the compiler needs to know everything about declarations and re-parse them again and again. 编译翻译单元时,编译器需要知道有关声明的所有内容并反复重新解析它们。 The definitions on the other hand can be compiled just once and reused for another units. 另一方面,定义只能编译一次,并重新用于其他单位。

A translation unit can be separated in .h and .cpp files. 翻译单元可以在.h.cpp文件中分隔。 You should put the declarations in .h and definitions in .cpp files to obey one definition rule. 您应该将.h的声明和.cpp文件中的定义放在一个定义规则中。 This approach also reduces compilation time. 这种方法也减少了编译时间。

Writing template-d classes and functions (without specialization), some coders (a bad habit in my opinion) will put the implementations in the .cpp files and they have to include them at the end of its corresponding .h file or in a .cpp file which needs them. 编写模板-d类和函数(没有专门化),一些编码器(我认为是一个坏习惯)会将实现放在.cpp文件中,并且必须将它们包含在相应的.h文件的末尾或者.cpp需要它们的.cpp文件。 It's just confusing. 这只是令人困惑。 A better naming convention is to rename these type of .cpp files to .impl.cpp and including them at the end of its .h file. 更好的命名约定是将这些类型的.cpp文件重命名为.impl.cpp ,并将它们包含在.h文件的末尾。

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

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