Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are not all Boost libraries header-only?

Tags:

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?

like image 445
Guillaume Paris Avatar asked Jul 06 '12 13:07

Guillaume Paris


People also ask

Is Boost library header-only?

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are: Boost.

Why are some libraries header-only?

Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a combination of headers and libraries ( lib , dll 's or . so files).

Is Boost ASIO header-only?

By default, Boost. Asio is a header-only library. However, some developers may prefer to build Boost. Asio using separately compiled source code.

Is the C++ Standard Library header-only?

Note the part of the C++ Standard Library which makes use of templates such as <vector> , string> , <map> , etc is header-only library. Actually templates (class templates and function templates) cannot be compiled into static or dynamic library to be linked to programs.


1 Answers

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.

like image 191
Sebastian Mach Avatar answered Dec 20 '22 00:12

Sebastian Mach