You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下fmt::print无法输出彩色文本的问题咨询

解决fmt::print无法输出彩色文本的问题

可能的原因及解决方案:

  • 显式启用颜色支持
    fmt 10.x版本默认会自动检测输出环境是否支持颜色,即便TERM设置正确,也可能需要手动强制启用颜色输出。可以通过两种方式实现:

    1. 调用fmt::print时搭配强调属性:
    #include <fmt/color.h>
    
    int main() {
        // 输出蓝色加粗文本
        fmt::print(fg(fmt::color::blue), fmt::emphasis::bold, "Test\n");
    }
    
    1. 设置全局颜色启用策略,确保后续所有fmt输出都支持颜色:
    #include <fmt/color.h>
    
    int main() {
        fmt::set_color_enabled(true);
        fmt::print(fg(fmt::color::blue), "Test\n");
    }
    
  • 检查编译配置
    确认编译代码时未禁用fmt的颜色支持。如果编译命令中定义了FMT_NO_COLOR宏,会直接关闭颜色输出,需移除该宏:

# 错误示例(禁用了颜色)
g++ your_code.cpp -o your_program -lfmt -DFMT_NO_COLOR

# 正确编译命令
g++ your_code.cpp -o your_program -lfmt
  • 确认输出环境未被重定向
    如果程序输出被重定向到文件或管道,fmt会自动禁用颜色。确保是直接在终端中运行程序,无输出重定向操作。

内容的提问来源于stack exchange,提问作者lei hu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:40:03