Linux下fmt库的安装和基础教程

安装

sudo apt-get install git 
 
cd /tmp && git clone https://github.com/fmtlib/fmt.git --depth=1

cd /tmp/fmt && mkdir build && cd build && cmake .. && make && sudo make install

使用

main.cpp

#include <fmt/core.h>
#include <fmt/chrono.h>
#include <fmt/ranges.h>
#include <fmt/os.h>
#include <fmt/color.h>
#include <iostream>
using namespace std;

int main() {

    // 打印到标准输出
    fmt::print("Hello, world!\n");

    // 格式化字符串
    std::string s1 = fmt::format("The answer is {}.", 42);
    // s == "The answer is 42."

    // 使用位置参数格式化字符串
    std::string s2 = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
    // s == "I'd rather be happy than right."

    // 打印日期和时间
    auto now = std::chrono::system_clock::now();
    fmt::print("Date and time: {}\n", now);
    fmt::print("Time: {:%H:%M}\n", now);
    
    // 使用颜色和文本样式进行打印
    fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
                "Hello, {}!\n", "world");
    fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
                fmt::emphasis::underline, "Olá, {}!\n", "Mundo");
    fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
                "你好{}!\n", "世界");


    // fmt::format
    int b = 2, c = 3, d = 4;
    string a = fmt::format(
        "\n==================\n"
        "a:{}\n""b:{}\n""c:{}\n" "d\n"
        "====================",
        "aaa", b, c, d
    );
    std::cout << a << endl;
    return 0;
}

CMakeLists.txt

  • CMakeLists.txt 与 main.cpp 同级
cmake_minimum_required(VERSION 3.15)
project(FMT_Test)
set(CMAKE_CXX_STANDARD 14)

find_package(FMT)
add_executable(run main.cpp)
target_link_libraries(run  fmt::fmt)

编译运行

mkdir build && cd build && cmake .. && make && ./run

运行结果如下
在这里插入图片描述
至此,你已经在你的Linux本地安装并成功编译运行代码了!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值