包括来自标题/源文件的文件 [英] Included file from header/source file

查看:111
本文介绍了包括来自标题/源文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于编译期间的性能,包括头文件中的文件或C ++中的源文件有什么区别?

What is the difference between including a file from a header file or from a source file in C++ in regards to performance during compilation?

包括哪些原因文件在源文件中而不在头文件中(除非绝对需要的话)?

What are the reasons to include files in the source file and not in the header file (except when absolutely required)?

一个(头)影响编译时间,另一个链接时间?

Does the one (header) affect compile time and the other (source file) affect link time?

推荐答案

当您在任一位置包含文件时,可以将其视为在文件中展开然后必须由预处理器和编译器处理。

When you include a file in either place, you can think of it as being expanded in the file, which then has to be processed by the preprocessor and compiler.

当您在标题中包含某些内容时,包含标题的每个客户端都会继承这些包含。因此,不必要地在头文件中包括文件有可能扩展几个翻译单元,这会对性能产生不利影响。

When you include something in your header, every client that includes your header inherits those includes. Thus, unnecessarily including files in headers has the potential to expand several translation units, which adversely affects performance.

这是一个很好的做法是将头包含限制为声明类。超越限制包括类中使用的那些类型,你也可以使用向前声明来代替仅通过指针或引用在类接口中使用的类型的包含。

It is good practice to limit header includes to those required to declare the class. Beyond limiting includes to those types used in a class, you may also use forward declarations in lieu of includes for types only used in the class interface by pointer or reference.

我的经验这种性能影响通常不明显。它可能在非常大的项目或广泛使用的标题中起作用。

In my experience this performance impact is usually not noticeable. It likely comes into play in very large projects or widely used headers.

这篇关于包括来自标题/源文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆