C++ 统一构建的好处/坏处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:34:13  来源:igfitidea点击:

The benefits / disadvantages of unity builds?

c++build-processcompilation

提问by Stowelly

Since starting at a new company I've noticed that they use unity cpp files for the majority of our solution, and I was wondering if anyone is able to give me a definitive reason as to why and how these speed up the build process? I would've thought that editing one cpp file in the unity files will force recompilation of all of them.

自从在一家新公司开始,我注意到他们在我们的大部分解决方案中使用了统一的 cpp 文件,我想知道是否有人能够给我一个明确的理由来说明为什么以及如何加速构建过程?我原以为在统一文件中编辑一个 cpp 文件会强制重新编译所有文件。

采纳答案by akent

Very similar question and good answers here: #include all .cpp files into a single compilation unit?

非常相似的问题和很好的答案:#include all .cpp files into a single compiler unit?

The summary seems to be that less I/O overhead is the major benefit.

总结似乎是减少 I/O 开销是主要的好处。

See also The Magic Of Unity Buildsas linked in the above question as well.

另请参阅上述问题中链接的The Magic Of Unity Builds

回答by mloskot

Lee Winder posted his experiences with the Unity Builds - The Evils of Unity Builds

Lee Winder 发布了他对 Unity Builds 的体验 - The Evils of Unity Builds

His conclusion is:

他的结论是:

Unity builds. I don't like them.

团结建立。我不喜欢他们。

回答by justin

It's because it saves redundant work. Redundant parsing and compilation for dependencies. Linking is also much more complex -- you either have your exports all in one object (or a few), or it's separate redundant exports across most of the target's object files. Fewer objects result in less I/O and reduced link times. Depending on your setup, inclusion could be a problem -- on the "unity build" system I use, the build is ultimately CPU and/or memory bound.

这是因为它节省了多余的工作。依赖项的冗余解析和编译。链接也更加复杂——您要么将导出全部放在一个(或几个)对象中,要么在大多数目标的对象文件中进行单独的冗余导出。更少的对象会导致更少的 I/O 和链接时间。根据您的设置,包含可能是一个问题——在我使用的“统一构建”系统上,构建最终受 CPU 和/或内存限制。