简体   繁体   English

使用多个文件C而不是将它们全部放入单个文件中时,二进制文件是否有所不同?

[英]Is there a difference in a binary when using multiple files C as opposed to putting it all into a single file?

I know that multiple files will by far make code easier. 我知道到目前为止,多个文件将使代码更容易。 However do they offer a performance difference between "jamming it all into one file" or will a modern compiler like gcc create the same binaries for both. 但是,它们是否在“全部塞入一个文件”之间提供了性能差异,还是像gcc这样的现代编译器会为两者创建相同的二进制文件。 When I say performance difference I mean file size, compile time, and running time. 当我说性能差异时,是指文件大小,编译时间和运行时间。

This is for C only. 这仅适用于C。

Arguably, compile times improve with multiple files, as you only need to recompile files that have changed (assuming you have a decent dependency-tracking build system). 可以说,使用多个文件可以缩短编译时间,因为您只需要重新编译已更改的文件(假设您有一个不错的依赖项跟踪构建系统)。

Linking would probably take longer, as there's just more to do. 链接可能需要更长的时间,因为还有更多工作要做。

Traditionally, compilers have been unable to perform optimizations across multiple source files (things like inlining functions is tricky). 传统上,编译器无法跨多个源文件执行优化(内联函数之类的东西很棘手)。 So the resulting executable is likely to be different, and potentially slower. 因此,生成的可执行文件可能会有所不同,并且可能会变慢。

There are more opportunities for optimization when everything is in a single file. 当所有内容都在一个文件中时,有更多的优化机会。 Eg gcc, starting with -O2 , will inline some functions if their body is available, even if they aren't declared inline (even more functions are eligible for inlining with -O3 ). 例如,以-O2开头的gcc会内联某些函数,即使它们的主体可用,即使它们没有被声明为内联(甚至更多的函数都可以使用-O3内联)。 So there are differences in run time, and sometimes you even have a chance to notice them. 因此,运行时间有所不同,有时您甚至有机会注意到它们。 Even more so with -fwhole-program , telling GCC that you don't care about out-of-line versions of external functions except main() (GCC behaves as if all your external functions became static). 使用-fwhole-program甚至可以告诉GCC您除了关心main()之外,不需要关心外部函数的离线版本(GCC的行为就像您所有的外部函数都变成静态的一样)。

Overall compile time may increase (because there is more stuff to analyze, and not all optimizer algorithms are linear) or decrease (when there's no need to parse the same headers multiple times). 整体编译时间可能会增加(因为要分析的东西更多,并且并非所有优化器算法都是线性的)或减少(当不需要多次解析相同的标头时)。 Binary size may increase (due to inlining, in exchange for running faster) or decrease (less likely; but sometimes, inlining simplifies caller's code to the point where code size decreases). 二进制大小可能会增加(由于内联,以换取更快的运行速度)或减小(可能性较小;但有时,内联会将调用者的代码简化到代码大小减小的程度)。

As of the ease of development and maintenance, you can use sqlite's approach: it has multiple source files, but they are jammed into one ("amalgamation") before compilation. 为了简化开发和维护,您可以使用sqlite的方法:它具有多个源文件,但是在编译之前将它们合并为一个(“合并”)。

From some tests, compiling and linking take longer. 从某些测试来看,编译和链接需要更长的时间。 You will receive a different binary, at least I did, however mine was within a byte of the other. 您将收到一个不同的二进制文件,至少我是这样做的,但是我的二进制文件在另一个字节的范围内。

The all-in-one file ran in .000764 MS The Multiple files version ran in .000769 MS Do take the benchmark with a grain of salt, as I did put it together in about 5 minutes, and it was a tiny program. 多文件版本运行于.000764 MS 。多个文件版本运行于.000769 MS 。就像我花了大约5分钟的时间就把它放在一起一样,这确实是一个很小的程序。

So really no differences overall. 因此,实际上总体上没有差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM