ANSI 颜色代码[重复]

问题描述 投票:0回答:0

当我在 Linux 上运行此命令时

$ gcc
,它给了我这样的输出:

gcc: fatal error: no input files
compilation terminated.

其中,

gcc
为粗体白色,
fatal error:
为粗体红色。

然后我运行了这个命令

$ gcc &> err.txt
err.txt的内容是:

gcc: fatal error: no input files
compilation terminated.

没有 ANSI 颜色代码,只有纯文本。

但是我的程序是用 C++ 编写的,文本文件的内容是:

[1;91merr:[0m no arguments were passed, try using '--help'

其中

err:
以粗体红色显示在终端中,但文件也包含这些字符。

我的示例代码是:

if (argc == 1)
{
    std::fprintf(stderr, "\033[1;91merr:\033[0m no arguments were passed, try using '--help'\n");
    return EXIT_FAILURE;
}

我的问题:

  1. gcc
    和其他Linux程序如何在不使用ANSI颜色代码的情况下在终端上打印彩色文本?
  2. 除了 ANSI 颜色代码之外,还有其他方法可以在终端上打印彩色输出吗?
  3. 有跨平台库可以这样做吗?
c++ gcc ansi ansi-colors
© www.soinside.com 2019 - 2024. All rights reserved.