有效的 C++“35.最小化文件之间的编译依赖性".今天还有效吗?
在这一章中,Scott Meyer 提到了一些避免头文件依赖的技术.如果更改仅限于其他包含的头文件,主要目标是避免重新编译 cpp 文件.
In this chapter Scott Meyer mentioned a few technique to avoid header files dependency. The main goal is to avoid recompiling a cpp file if changes are limited to other included header files.
我的问题是:
在我过去的项目中,我从未关注过这条规则.编译时间不短但也不是不能忍受.这可能与我的项目的规模(或缺乏)有关.鉴于编译器技术(例如 clang)的进步,今天这个技巧有多实用?
In my past projects I never paid attention to this rule. The compilation time is not short but it is not intolerable. It could have more to do with the scale (or the lack of) of my projects. How practical is this tip today given the advance in the compiler technology (e.g. clang)?
在哪里可以找到更多使用此技术的示例?(例如 Gnome 或其他 OSS 项目)
Where can I find more examples of the use of this techniques? (e.g. Gnome or other OSS projects)
附:我正在使用第 2 版.
P.S. I am using the 2nd edition.
推荐答案
我不认为编译器技术特别先进.clang 并不是什么神奇的东西――如果你有依赖然后你做了改变,那么依赖的代码将不得不重新编译.这可能需要非常非常长的时间――阅读一个大项目需要几个小时甚至几天,因此人们会尽可能减少这种依赖关系.
I don't think compiler technology has advanced particularly. clang is not some piece of magic - if you have dependencies then and you make changes, then dependent code will have to be recompiled. This can take a very, very long time - read hours, or even days for a big project, so people try to minimise such dependencies where possible.
话虽如此,有可能做的太多了――将所有类都变成 PIMPL,向前声明所有内容等.这样做只会导致代码混淆,应尽可能避免.
Having said that, it is possible to overdo things - making all classes into PIMPLs, forward declaring everything, etc. Doing this just leads to obfuscated code, and should be avoided whenever possible.
相关文章