ein*_*ica 3 c++ design-rationale rationale header-only fmt
我知道可以fmt在仅标头模式下使用格式化库:
但是 - 为什么不只是标题,句号?也就是说,在 non-header-only 模式下使用它有什么好处?
主要原因是其他人已经正确指出的构建速度。例如,使用静态库(默认)进行编译比使用只有头文件的库快 2.75 倍:
#include <fmt/core.h>
int main() {
fmt::print("The answer is {}.", 42);
}
Run Code Online (Sandbox Code Playgroud)
% 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
Run Code Online (Sandbox Code Playgroud)
在仅标头库中,实现细节和依赖项会泄漏到使用它们的每个翻译单元中。
在非标头模式下使用它有什么好处?
我不是作者,所以我不能代表他们说话。但我可以告诉你 not-header-only 的优点。