首页 > 解决方案 > 为什么 fmt 库不是仅标头?

问题描述

我知道可以fmt在仅标题模式下使用格式化库:

如何在仅标头模式下使用 fmt 库?

但是 - 为什么不只是标题,句号?也就是说,在 non-header-only 模式下使用它有什么好处?

标签: c++fmtheader-onlyrationaledesign-rationale

解决方案


正如其他人已经正确指出的那样,主要原因是构建速度。例如,使用静态库(默认)编译比仅使用标头库快约 2.75 倍:

#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

在仅标头库中,实现细节和依赖关系泄漏到使用它们的每个翻译单元中。


推荐阅读