单个源代码与多个文件 +图书馆
拥有多个文件或编译库与将所有内容(> 10,000 LOC)放入一个源中对最终二进制文件有多大影响?例如,我没有单独链接 Boost 库,而是将其代码与原始源代码一起粘贴到一个巨大的文件中进行编译。同样,不要将多个文件输入到 gcc 中,而是将它们全部粘贴在一起,然后只给出一个文件。
我对优化差异感兴趣,而不是维护庞大的单个源文件所带来的问题(恐怖)。
当然,只能进行链接时优化(我可能是错的),但是优化可能性之间有很大差异吗?
How much effect does having multiple files or compiled libraries vs. throwing everything (>10,000 LOC) into one source have on the final binary? For example, instead of linking a Boost library separately, I paste its code, along with my original source, into one giant file for compilation. And along the same line, instead of feeding several files into gcc
, pasting them all together, and giving only that one file.
I'm interested in the optimization differences, instead of problems (horror) that would come with maintaining a single source file of gargantuan proportions.
Granted, there can only be link-time optimization (I may be wrong), but is there a lot of difference between optimization possibilities?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果编译器可以看到所有源代码,并且您的编译器具有某种过程间优化,那么它可以更好地优化(IPO)选项已启用。 IPO 与其他编译器优化不同,因为它分析整个程序;其他优化仅关注单个函数,甚至单个代码块
以下是一些可以完成的过程间优化,请参阅此处了解更多信息:
GCC 支持这种优化。
这种过程间优化可以用来分析和优化被调用的函数。
如果编译器看不到库函数的源代码,则无法进行此类优化。
If the compiler can see all source code, it can optimize better if your compiler has some kind of Interprocedural Optimization (IPO) option turned on. IPO differs from other compiler optimization because it analyzes the entire program; other optimizations look at only a single function, or even a single block of code
Here is some interprocedural optimization that can be done, see here for more:
GCC supports this kind of optimization.
This interprocedural optimization can be used to analyze and optimize the function being called.
If compiler can not see the source code of the library function, it cannot do such optimization.
请注意,一些现代编译器(clang/LLVM、icc 以及最近的 gcc)现在支持链接时优化 (LTO),以最大限度地减少单独编译的影响。因此,您可以获得单独编译(维护、更快的编译等)和整个程序分析的好处。
顺便说一句,gcc 从 4.1 版本开始似乎已经支持 -fwhole-program 和 --combine 了。不过,您必须将所有源文件一起传递。
最后,由于 BOOST 主要是 #included 的头文件(模板),因此您无法通过将这些文件添加到源代码中获得任何好处。
Note that some modern compilers (clang/LLVM, icc and recently even gcc) now support link-time-optimization (LTO) to minimize the effect of separate compilation. Thus you gain the benefits of separate compilation (maintenance, faster compilation, etc.) and these of whole program analysis.
By the way, it seems like gcc has supported -fwhole-program and --combine since version 4.1. You have to pass all source files together, though.
Finally, since BOOST is mostly header files (templates) that are #included, you cannot gain anything from adding these to your source code.