安装
- 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)
参考链接