安裝
- git clone??https://github.com/fmtlib/fmt.git
- make .
- mkae? &&? make install
CLion使用
- 使用和安裝存在出入
- 下載源碼,可以先 clone 到你的項目中去,https://github.com/fmtlib/fmt ,我放到的是項目的 dependencies 目錄
- 然后在 CMake 中加上這兩句:
add_subdirectory(dependencies/fmt EXCLUDE_FROM_ALL)
target_link_libraries(fmt_demo fmt-header-only)
- 其中?
EXCLUDE_FROM_ALL
?表示將這個項目移除出 make 列表。 - 接著是鏈接 fmt-header-only 這個庫,使用? ?借用源碼 和 生成的庫文件 ,不要最后一步??mkae? &&? make install
?具體例子
屏幕截圖

main.cpp
#include <string>
#include <cstdio>
#include "dependencies/fmt/include/fmt/core.h"int execute_command(const std::string &command,std::string *output = nullptr,bool redirect_stderr = false){const auto &cmd = redirect_stderr ? command + " 2>&1" : command;auto pipe = popen(cmd.c_str(),"r");if (!pipe){//記錄日志return -1;}{char buffer[1024] = {0};while(fgets(buffer,sizeof (buffer),pipe) != nullptr){
// if (output){
// output->append(buffer);
// }printf("%s",buffer);}}return pclose(pipe);
}int main(){
// FILE *fp;
// char buffer[1024] = {0};
// fp = popen("cat /etc/passwd","r");
// fgets(buffer,sizeof(buffer),fp);
// printf("%s",buffer);
// pclose(fp);std::string shell_command = {" /etc/passwd"};return (execute_command(fmt::format("cat {0} 2>/dev/null",shell_command))) == 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(learning)set(CMAKE_CXX_STANDARD 14)add_executable(learning main.cpp)add_subdirectory(dependencies/fmt EXCLUDE_FROM_ALL)target_link_libraries(learning fmt-header-only)
參考鏈接
- c++ fmt 庫安裝和使用示例
- c++使用fmt::format格式化字符串
- Fmt:更方便的 c++ format 庫
- c++使用fmt::format格式化字符串