是不必要的包含文件的开销? [英] Are unnecessary include files an overhead?

查看:91
本文介绍了是不必要的包含文件的开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些关于如何在C ++项目中检测不必要的#include文件的问题。这个问题常常吸引我,但我从来没有找到满意的答案。

I have seen a couple of questions on how to detect unnecessary #include files in a C++ project. This question has often intrigued me, but I have never found a satisfactory answer.

如果有一些头文件包括在c ++项目中,而不是在使用,是一个开销?我理解这意味着在编译之前,所有头文件的内容将被复制到包含的源文件,这将导致大量的不必要的编译。

If there are some header files included which, are not being used in a c++ project, is that an overhead? I understand that it means that before compilation the contents of all the header files would be copied into the included source files and that would result in a lot of unnecessary compilation.

如何这样的开销是否传播到编译的目标文件和二进制文件?

How far does this kind of overhead spread to the compiled object files and binaries?


编译器不能做一些优化,这个
种类的开销不会传递到生成的目标文件和
二进制文件?

Aren't compilers able to do some optimizations to make sure that this kind of overhead is not transferred to the resulting object files and binaries ?

考虑到这一事实,我可能不知道编译器优化,我还是想问这个,万一有一个答案。

Considering the fact, that I probably know nothing about compiler optimization, I still want to ask this, in case there is an answer.


作为一个程序员,他的工作使用了各种各样的c ++库,
我应该遵循什么样的编程习惯保持避免
这样的开销?

As a programmer who uses a wide variety of c++ libraries for his work, what kind of programming practices should I follow to keep avoiding such overheads ? Is making myself intimately familiar with each library's working the only way out ?


推荐答案

它不影响二进制文件的性能,甚至二进制文件的内容,几乎所有头。 声明根本不生成代码,如果不使用inline / static / anonymous-namespace定义,它们将被优化,并且没有头应该包含外部可见的定义(如果头部包含更多比一个翻译单位)。

It does not affect the performance of the binary or even the contents of the binary file, for almost all headers. Declarations generate no code at all, inline/static/anonymous-namespace definitions are optimized away if they aren't used, and no header should include externally visible definitions (that breaks if the header is included by more than one translation unit).

作为@TC指出,异常是内部可见的静态对象与非平凡构造函数。 iostream 这样做,例如。程序必须像调用构造函数一样工作,编译器通常没有足够的信息来优化构造函数。

As @T.C. points out, the exception are internally visible static objects with nontrivial constructors. iostream does this, for example. The program must behave as if the constructor is called, and the compiler usually doesn't have enough information to optimize the constructor away.

但是,它会影响多长时间当头更改时,编译需要和将重新编译多少文件。对于大型项目,这是足够的鼓励,以关心不必要的包括。

It does, however, affect how long compilation takes and how many files will be recompiled when a header is changed. For large projects, this is enough incentive to care about unnecessary includes.

这篇关于是不必要的包含文件的开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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