fmt5版本之后使用了C++11的一些特性,之前的版本支持C++98,目前最新版本为4.1.0,支持C++98的版本会持续更新,不必担心bug修复问题
以下为fmt4.1.0版本的简单使用教程
首先下载4.1.0的release版本,https://github.com/fmtlib/fmt/releases/download/4.1.0/fmt-4.1.0.zip
新建一个fmtTest项目,将fmt-4.1.0.zip中的fmt文件夹全部复制到fmtTest项目下,
控制台项目代码如下:
#include "stdafx.h" #include <iostream> using namespace std; #include ".\fmt410\format.h" int _tmain(int argc, _TCHAR* argv[]) { std::string str = fmt::format("The answer is {}", 42); cout << "result=" << str << endl; getchar(); return 0; }
这样即可正常打印出结果。
为什么加载
#include ".\fmt410\format.h" fmt::format("The answer is {}", 42);
可参见fmt-4.1.0.zip\fmt-4.1.0\doc\html\index.html中的一些说明。
操作视频可见fmt4.1.0使用教程.wmv
Fmt的安装与使用,最主要是吐槽。
转自https://blog.csdn.net/qq_28691955/article/details/108152442
安装
本文的编译平台的是VS2015 与Cmake.
1、打开Cmake,在1处选择下载的源码,
2、在2处选择生成文件所放在的位置。需要自己在目录下新建build文件夹。
3、点击3处生成配置。注意第一次点击的时候会出现一个对话框,让你选择编译平台。我选的是VS2015 X64平台。默认不选的话会是x86的。
4、第一次点击3处按钮可能会出现红色的,如图所示。再点击一次3处按钮。
5、点击4处按钮,在build文件夹生成项目文件。
6、点击下面的fmt项目,右键重新生成,生成库文件,注意是release还是debug模式,根据需要自己选择。
7、至此,库文件生成成功,将头文件与库文件连接好在别的项目就可以使用。
使用
部分使用情况
#include <iostream> #include <vector> #include <string> #include "fmt/ranges.h" #include "fmt/core.h" #include "fmt/color.h" #include "fmt/chrono.h" #include "fmt/os.h" #include "fmt/printf.h" #include "fmt/compile.h" #include "fmt/locale.h" //#include "fmt/posix.h" // 使用 memory_buffer 包含的头文件 #include "fmt/format.h" /* 静态库必须这么写 */ #ifndef _WIN64 #ifdef _DEBUG #pragma comment(lib, "../lib32/debug/fmtd.lib") #else #pragma comment(lib, "../lib32/release/fmt.lib") #endif #else #ifdef _DEBUG #pragma comment(lib, "../lib64/debug/fmtd.lib") #else #pragma comment(lib, "../lib64/release/fmt.lib") #endif #endif // _a前缀必须引用的命名空间 using namespace fmt::literals; /* 参考文档 https://www.cnblogs.com/pandamohist/p/13362139.html */ int main() { //fmt::print("Hello, world!\n"); /*std::vector<int> v = { 1, 2, 3 }; fmt::print("{}\n", v);*/ /*using namespace std::literals::chrono_literals; fmt::print("Default format: {} {}\n", 42s, 100ms); fmt::print("strftime-like format: {:%H:%M:%S}\n", 3h + 15min + 30s);*/ //std::string s = fmt::format("The answer is {}.", 42); //std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy"); //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, "Hello, {}!\n", "мир");*/ //fmt::print(fg(fmt::rgb(255,20,30)),"Hello, {}!\n", "世界"); //fmt::ostream::output_file(); /*auto out = fmt::output_file("guide.txt"); out.print("Don't {}", "Panic");*/ // 1、fmt::format 返回一个字符串 /*auto str = fmt::format("1、{0} = {1}\n\n", "1+1", 2); std::cout << str << std::endl;*/ // 2、使用 fmt::memory_buffer 可以避免构造使用string /* // 使用 memory_buffer 包含的头文件 #include "3rd_part/fmt/format.h" */ //fmt::memory_buffer out; //format_to(out, "2、For a moment, {} happened.\n\n", "nothing"); 这里输出的也是一个char字符串 //fmt::print(out.data()); //auto err_no = 1; //fmt::print(stderr, "3、System error code = {}\n\n", err_no); //fmt::print("3、Don't {}\n\n", "panic"); //fmt::print("4、I'd rather be {1} than {0}.\n\n", "right", "happy"); //fmt::print("5、Hello, {name}! The answer is {number}. Goodbye, {name}.\n\n", //fmt::arg("name", "World"), //fmt::arg("number", 99)); ///* //若编译器支持c++ 11用户自定义的文本, 后缀 _a提供了一种可以选。 //*/ //fmt::print("6、Hello, {name}! The answer is {number}. Goodbye, {name}.\n\n", //"name"_a = "World", //"number"_a = 42); // fmt::print(fmt::format(L"Cyrillic letter {}", L'\x42e')); fmt::print(bg(fmt::terminal_color::cyan), "tcyan"); //fmt::print(fg(fmt::rgb(10, 50, 63)), "hello {}\n", "world"); //fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) | //fmt::emphasis::underline, "Hello, {}!\n", "мир"); //fmt::print(fg(fmt::rgb(255, 20, 30)), "Hello, {}!\n", "世界"); system("pause"); return 0; }
吐槽
在我的测试项目里面,打印带颜色的字符会出错,向文件写字符也会出错。我在测试项目里面注释掉了,各位可以打开注释自己试一下。不知道是不是我是Window平台下不能使用。有知道的大佬可以在评论留言教我一下。烦的一批啊。
文章评论