Why are all libraries in Boost not headers-only? Saying it differently, what makes the use of .lib/.dll mandatory?
Is it when a class can't be a template or has static fields?
See Question&Answers more detail:osWhy are all libraries in Boost not headers-only? Saying it differently, what makes the use of .lib/.dll mandatory?
Is it when a class can't be a template or has static fields?
See Question&Answers more detail:osDifferent points, I guess.
and a bit of security
If there's a lot of reachable code in the boost library, or code about which the compiler can't argue whether it is reachable by the client, it has to be put into the final binary. (*)
On operating systems that have package management (e.g. RPM- or .deb-based), shared libraries can mean a big decrease in binary distribution size and have a security advantage: Security fixes are distributed faster and are then automatically used by all .so/.DLL users. So you had one recompile and one redistribution, but N profiteers. With a header-only library, you have N recompiles, N redistributions, always for each fix, and some member of those N are huge in themselves already.
(*) reachable here means "potentially executed"
Some boost libraries are huge. If you would #include
it all, each time you change a bit in your source-file, you have to recompile everything you #include
d.
This can be counter-measured with cherry picked headers, e.g.
#include <boost/huge-boost-library.hpp> // < BAD
#include <boost/huge-boost-library/just-a-part-of-it.hpp> // < BETTER
but sometimes the stuff you really need to include is already big enough to cripple your recompiles.
The countermeasure is to make it a static or shared library, in turn meaning "compile completely exactly once (until the next boost update)".
We are still not in an age were global optimization solves all of our C++ performance problems. To make sure you give the compiler all the information it needs, you can make stuff header-only and let the compiler make inlining decisions.
In that respect, note that inlining gives not always superior performance because of caching and speculation issues on the CPU.
Note also that this argument is mostly with regards to boost libraries that might be used frequently enough, e.g. one could expect boost::shared_ptr<>
to be used very often, and thus be a relevant performance factor.
But consider the real and only relevant reason boost::shared_ptr<>
is header-only ...
Some stuff in C++ can not be put into libraries, namely templates and enumerations.
But note that this is only halfway true. You can write typesafe, templated interfaces to your real data structures and algorithms, which in turn have their runtime-generic implementation in a library.
Likewise, some stuff in C++ should be put into source files, and in case of boost, libraries. Basically, this is everything that would give "multiple definition" errors, like static
member variables or global variables in general.
Some examples can also be found in the standard library: std::cout
is defined in the standard as extern ostream cout;
, and so cout
basically requires the distribution of something (library or sourcefile) that defines it once and only once.