惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
量子位
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Jina AI
Jina AI
L
LangChain Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Pretty Output Format in C++
2021-07-04 · via jdhao's digital space

Although std::cout in C++ is powerful, it lacks the ease of use of format string in Python. Fortunately, the 3rd party package fmt provides a similar feature for C++.

Build fmt#

To use the fmt package, we need to build it:

git clone --depth 1 https://github.com/fmtlib/fmt.git
cd fmt
mkdir build && cd build
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=~/tools/fmt -DCMAKE_BUILD_TYPE=Release ..
make -j 8
make install

This will build the static version of fmt and install it under $HOME/tools/fmt.

To build a dynamic library, use the following cmake command instead:

cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=~/tools/fmt -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=TRUE ..

How to use#

Here is a short snippet to test it:

#include <iostream>
#include <vector>
#include <fmt/ranges.h>
#include <fmt/core.h>

using std::vector;

int main() {
  vector<int> arr = {1, 2, 3};

  fmt::print("arr is {}", arr);

  fmt::print("The price of {} is {} Yuan/kg", "apple", 4.2);
  return 0;
}

Use the following comamnd to build against the static version of fmt:

g++ -L$HOME/tools/fmt/lib64/ -I$HOME/tools/fmt/include/ test.cc -l:libfmt.a -o test_static

And the following command to build against the dynamic version of fmt:

g++ -L$HOME/tools/fmt/lib64/ -I$HOME/tools/fmt/include/ test.cc -lfmt -o test_shared

To run the dynamic executable, you need to set up the env variable LD_LIBRARY_PATH properly:

LD_LIBRARY_PATH=$HOME/tools/fmt/lib64:$LD_LIBRARY_PATH ./test_shared

The size differs significantly between the static and dynamic executable files:

32K     test_shared
220K    test_static

References#