c++ fmt 库安装和使用示例、clion配置

54 篇文章 18 订阅

安装

  • 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)

参考链接

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值