Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

c++ - Why are not all Boost libraries header-only?

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:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Different points, I guess.

  • Binary size. Could header-only put a size burden on the client?
  • Compilation times. Could header-only mean a significant decrease in compilation performance?
  • Runtime Performance. Could header-only give superior performance?
  • Restrictions. Does the design require header-only?

About binary size.

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"

About compilation times.

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 #included.

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)".

About runtime performance.

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 ...

About restrictions.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...