How come the fmt library is not header-only?
Asked Answered
E

3

7

I know it is possible to use the fmt formatting library in header-only mode:

How to use fmt library in the header-only mode?

but - why isn't it just header-only, period? That is, what's the benefit of using it in non-header-only mode?

Elmore answered 15/6, 2021 at 20:32 Comment(0)
T
15

The main reason is build speed as others already correctly pointed out. For example, compiling with a static library (the default) is ~2.75x faster than with a header-only one:

#include <fmt/core.h>

int main() {
  fmt::print("The answer is {}.", 42);
}
% time c++ -c test.cc -I include -std=c++11
c++ -c test.cc -I include -std=c++11  0.27s user 0.05s system 97% cpu 0.324 total

% time c++ -c test.cc -I include -std=c++11 -DFMT_HEADER_ONLY
c++ -c test.cc -I include -std=c++11 -DFMT_HEADER_ONLY  0.81s user 0.07s system 98% cpu 0.891 total

In header-only libraries implementation details and dependencies leak into every translation unit that uses them.

Toplevel answered 17/6, 2021 at 13:20 Comment(0)
S
7

Some functions like vformat are not templates. There is no point in putting those in headers and slowing down the whole compilation process. I would guess that's the rationale. The fmt library cares a lot about the compilation time from what I can tell.

Sapient answered 15/6, 2021 at 20:58 Comment(0)
O
7

what's the benefit of using it in non-header-only mode?

I'm not the author, so I cannot speak for them. But I can tell you advantages of not-header-only.

  • Allows the headers of the library to not include system specific headers, which generally have problems such as defining macros with names that may conflict with the user program (for example windows.h which may define macros that overlap with certain standard libary functions). Libfmt does in fact take advantage of this opportunity. The OS specific functionality is omitted in the header-only mode unless I'm mistaken.
  • Non-inline functions generally allow for much faster re-builds in case the implementation changes. This is very relevant to the library developer who probably does that a lot. It could be relevant to the user if the library is updated often. The importance of this increases as the library grows. This cannot be taken advantage of in case of unbounded template code. Most of these conditions are probably in the "not very relevant or cannot be taken advantage of" side for libfmt, which is probably why it offers the option of header-only in the first place.
Ordinand answered 15/6, 2021 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.