【发布时间】:2021-08-31 17:03:11
【问题描述】:
我知道可以在仅标题模式下使用fmt 格式化库:
How to use fmt library in the header-only mode?
但是 - 为什么不只是标题,句号?也就是说,在 non-header-only 模式下使用它有什么好处?
【问题讨论】:
标签: c++ fmt header-only rationale design-rationale
我知道可以在仅标题模式下使用fmt 格式化库:
How to use fmt library in the header-only mode?
但是 - 为什么不只是标题,句号?也就是说,在 non-header-only 模式下使用它有什么好处?
【问题讨论】:
标签: c++ fmt header-only rationale design-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
在只有头文件的库中,实现细节和依赖项会泄漏到每个使用它们的翻译单元中。
【讨论】:
像vformat 这样的一些函数不是模板。将它们放在标题中并减慢整个编译过程是没有意义的。我想这就是理由。据我所知,fmt 库非常关心编译时间。
【讨论】:
在非仅标头模式下使用它有什么好处?
我不是作者,所以我不能代表他们说话。但我可以告诉你 not-header-only 的优点。
【讨论】: