Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Boost mean by "header-only libraries" and "automatic linking"?

On the Boost library documentation page, there are two categories named "Header Only Libraries" and "Automatic Linking".

I suppose "Header Only Libraries" means you don't have to link against Boost libraries in order to use them, and "Automatic Linking" means you have to link.

But when I use Boost.Timer, I have to link a static or dynamic library named timer (libboost_timer.a and libboost_timer.so.1.48.0 and various soft links to these under Linux library path), which is apparently the exact library file of Boost.Timer. I even need to link against Boost.System and Boost.Chrono, though it is understandable that the library itself uses some other libraries that need to be linked.

On the other side, Boost has clearly stated that Boost.Asio belongs to "Automatic Linking", but there aren't any library files named anything like asio.

So what does it actually mean to be a "header-only library" or "automatic linking"? Or is it purely a mistake?

like image 225
WiSaGaN Avatar asked Aug 02 '12 01:08

WiSaGaN


People also ask

Is Boost a header-only library?

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.

What means header-only library?

In the context of the C or C++ programming languages, a library is called header-only if the full definitions of all macros, functions and classes comprising the library are visible to the compiler in a header file form.

Are header-only libraries good?

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

What does the Boost library do?

Boost is a set of libraries for the C++ programming language that provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains 164 individual libraries (as of version 1.76).


2 Answers

As you said, "Header only library" means that the whole library is in header files, so one (or several) #include lines is enough to use it. No linking is necessary.

"Automatic linking" means that, although the library needs some linking (either directly or as a dependency), you don't need to specify it in the compiler line, because the #include'd files will do some magic to bring in the appropriate libraries automatically, if supported by the compiler.

For example, in MSVC compilers, they use #pragman comment(lib, "..."); in Borland compilers they use #pragma defineoptions;, etc.

And most notably, "automatic linking" is not supported by the GNU compiler.

Automatic linking can be troublesome sometimes (for example, mixing debug and release versions), and you can selectively disable them by defining some preprocessor macros: BOOST_<libname>_NO_LIB. In that case you will have to do the linking manually.

UPDATE: About your comment below:

Boost.Timer claims to be a "Header only library" but it has lib files in the lib directory.

It looks like there is an error in the Boost documentation. Actually there are two different libraries named timer: The old, deprecated, header-only <boost/timer.hpp> and the new, improved, cooler, automatically linkable <boost/timer/timer.hpp>.

But for some reason, the main documentation page lists the properties of the old one.

There's no Boost.Asio lib files.

In the main Boost library documentation page library documentation page, you can see that Asio is listed as Automatic linking due to dependency. The specific dependencies are listed elsewhere: Boost.System and Boost.Regex, and both present automatic linking.

like image 117
rodrigo Avatar answered Sep 21 '22 13:09

rodrigo


You've pretty much nailed it -- a header only library is one where all the code for that library is contained in the header(s), so you only have to include them, not link against a library to use them.

That said, it's entirely possible to write a header-only library that depends on some other library, which may not be of the header-only variety. In this case, even though you don't have to tell the linker about the first library you're using, you still have to tell it about the second. Especially when/if all the code might be stuffed into one of what the linker thinks of as a library (e.g., one .lib or .a file), that may end up mostly a distinction without a difference (just to be clear: that isn't necessarily the case here, but it can and does arise anyway).

like image 31
Jerry Coffin Avatar answered Sep 20 '22 13:09

Jerry Coffin