NOhs
NOhs

Reputation: 2830

What is the reason for a library to not be header-only?

I really like using header-only libraries, as they are really easy to use (no linker issues or having to compile the library separately). For example most of the Boost library is header-only. But then again there are some parts, like boost::python, which requires to be build before. Is this a design choice or a technical necessity?

I gave Boost as an example but would appreciate a more general answer if possible.

Upvotes: 3

Views: 365

Answers (2)

dascandy
dascandy

Reputation: 7292

In favor of header only:

  • Less time to set up / import
  • No linking trouble

Against header only:

  • Collisions between headers unresolvable with compile-time firewall between them
  • No dependency hiding possible
  • Increases compile time for each target that includes its headers
  • Adds to symbol pile exported from target. Your simple test file may now export a few thousand symbols.
  • (at extremes) May reduce separation between COMDAT-foldable symbols, causing multiple copies of your symbols to be irremovable from the target output files, causing bloat. This should only happen at 32k+ symbols on ELF, but happens much earlier on other targets (such as Mach-O).

Upvotes: 1

user1196549
user1196549

Reputation:

The original reason to use compiled libraries is to spare compilation time. Libraries can be big. They can be huge.

Another argument is that they keep the source code apart. There is still a good deal of the universe which is not open-sourced.

Upvotes: 7

Related Questions