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?
-
Currently using filesystem, lexical_cast, random, and bind. – Anonymous Jan 23 '10 at 03:07
-
2Don't accept an answer so soon. Some people haven't even gotten to the question. This is no offense to dsimcha, just give others a chance. :) – GManNickG Jan 23 '10 at 03:09
-
Sorry, but once he said what he said it just kind of clicked. If someone responds with a better I change answers. I still regularly check the question after I consider it "answered", but for now, dsimcha's answer is sufficient for me. – Anonymous Jan 23 '10 at 03:10
-
2Indeed, but also keep in mind some people avoid answered question (which I don't advise they do, but still.) Leaving it unanswered gives others a motive to give you an answer. – GManNickG Jan 23 '10 at 03:14
-
Are you compiling in "debug mode"? If so, try compiling without debug information and with optimization / strip symbols turned on. – Emile Cormier Jan 23 '10 at 17:21
4 Answers
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.

- 67,514
- 53
- 213
- 334
-
3"not all linkers are sufficiently smart". Being so dumb as to leave in "multiple copies" of the same code is also sometimes referred to as "inlining" ;-). Seriously, though, templates are easier to inline than static libraries. Unless you set the options to only inline when it won't increase the code size, it'll increase the code size. That's even before any straight-up inefficiencies like duplicated out-of-line code. – Steve Jessop Jan 23 '10 at 03:24
-
3You will get a copy in every single obj file that references each header, as well. Since most of these templates are implemented on top of other templates, multiple each template by the number of templates it uses as well. You can easily get tens of thousands of duplicates of cout templates alone in a big project. And the way stl and boost are implemented, code is monstrously bloated anyhow. Plus mem fragmentation and optimization killing happens like crazy because there's so many func ptrs and smt ptrs. In short, don't use boost at all, and use stl as sparingly as possible. – Charles Eli Cheese Jan 23 '10 at 04:15
-
@Charles: Absolutely terrible general advice. Unless you specifically mean to lower code size (which seriously doesn't matter much anymore), you should use them as much as possible. – GManNickG Jan 23 '10 at 20:00
"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.

- 494,350
- 52
- 494
- 543
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.

- 56,922
- 16
- 83
- 148
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.

- 453
- 4
- 6
-
From my experience, if you have to use Boost, maybe you need to use a different language with more of the intrinsic features boost offers, like properties, etc... Boost adds significantly, sometimes almost to a crippling degree to the build-time of a project. – Jordan Dec 19 '16 at 12:23