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?
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?
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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.