c++fmtheader-only

How come the fmt library is not header-only?


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?


Solution

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