安装
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本地安装并成功编译运行代码了!!!