为什么使用 boost 会增加文件大小这么多?
我注意到,当我使用增强功能时,应用程序大小往往会增加约 0.1 - 0.3 MB。这可能看起来不多,但与使用其他外部库相比(至少对我来说)。这是为什么呢?
I've noticed that when I use a boost feature the app size tends to increase by about .1 - .3 MB. This may not seem like much, but compared to using other external libraries it is (for me at least). Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Boost到处都使用模板。这些模板可以使用相同的参数多次实例化。一个足够聪明的链接器将丢弃除一个副本之外的所有副本。然而,并非所有链接器都足够智能。此外,模板有时会隐式实例化,甚至很难知道一个模板已被实例化了多少次。
Boost uses templates everywhere. These templates can be instantiated multiple times with the same parameters. A sufficiently smart linker will throw out all but one copy. However, not all linkers are sufficiently smart. Also, templates are instantiated implicitly sometimes and it's hard to even know how many times one has been instantiated.
“这么多”是一个比较术语,恐怕您正在将苹果与橙子进行比较。仅仅因为其他库较小并不意味着您应该假设 Boost 也一样小。
看看 Boost 为您所做的巨大工作量!
我怀疑制作具有相同功能的自定义库的大小是否会小得多。唯一有效的比较是“Boost 的库执行 X”与“另一个执行 X 的库”。不是“Boost 的库执行 X”和“另一个执行 Y 的库”。
文件系统库非常强大,这意味着有大量的功能和大量的骨干代码来为您和我提供一个简单的界面。另外,就像其他人提到的那样,模板通常会增加代码大小,但这并不是一件可以避免的事情。模板或手工编码,任何一种都会产生相同大小的代码。唯一的区别是模板更容易。
"so much" is a comparative term, and I'm afraid you're comparing apples to oranges. Just because other libraries are smaller doesn't imply you should assume Boost is as small.
Look at the sheer amount of work Boost does for you!
I doubt making a custom library with the same functionality would be of any considerable lesser size. The only valid comparison to make is "Boost's library that does X" versus "Another library that does X". Not "Boost's library that does X" and "Another library that does Y."
The file system library is very powerful, and this means lots of functions, and lot's of back-bone code to provide you and I with a simple interface. Also, like others mentioned templates in general can increase code size, but it's not like that's an avoidable thing. Templates or hand-coded, either one will results in the same size code. The only difference is templates are much easier.
这完全取决于它的使用方式。由于 Boost 是一堆模板,因此它会导致根据使用的类型编译一堆成员函数。如果将 boost 与 n 类型一起使用,则成员函数将被定义(由 C++ 模板)n 次,每种类型一次。
It all depends on how it is used. Since Boost is a bunch of templates, it causes a bunch of member functions to be compiled per type used. If you use boost with n types, the member functions are defined (by C++ templates) n times, one for each type.
Boost 主要由非常通用且有时相当复杂的模板组成,这意味着类型和函数是由编译器根据使用需要创建的,而不仅仅是通过声明创建的。换句话说,少量的源代码可以产生大量的目标代码来满足声明或使用的模板的所有变体。 Boost 还依赖于标准库,也引入了这些依赖项。然而,最重要的贡献是 Boost 源代码几乎主要存在于包含文件中。包含标准 c 包含文件(STL 之外)通常包含很少的源代码,并且主要包含原型、小宏或类型声明,而没有它们的实现。 Boost 在其包含文件中包含其大部分实现。
Boost consists primarily of very generalized and sometimes quite complex templates, which means, types and functions are created by the compiler as required per usage, and not simply by declaration. In other words, a small amount of source code can produce a significant quantity of object code to fulfill all variations of templates declared or used. Boost also depends on standard libraries, pulling in those dependencies as well. However, the most significant contribution is the fact that Boost source code exists almost primarily in include files. Including standard c include files (outside of STL) typically includes very little source code and contains mostly prototypes, small macros or type declarations without their implementations. Boost contains most of its implementations in its include file.