C++ 改进 g++ 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5732562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Improving g++ output
提问by slezica
g++ sometimes produces pretty convoluted outputs. Specially when dealing with templates. Is there any tool that makes g++ output more readable? ... at least some color?
g++ 有时会产生非常复杂的输出。特别是在处理模板时。是否有任何工具可以使 g++ 输出更具可读性?...至少有一些颜色?
It may sound silly to ask this question here, but I've been unable to google my way out of this.
在这里问这个问题可能听起来很愚蠢,但我一直无法通过谷歌搜索解决这个问题。
采纳答案by Darhuuk
If you're free to switch compiler, try clang++. It's error outputs are most of the time very clear and sometimes it will even tell you what to do to fix your error. You can find a comparison of gcc vs clang error output here: http://clang.llvm.org/diagnostics.html.
如果您可以自由切换编译器,请尝试clang++。它的错误输出大部分时间都非常清楚,有时它甚至会告诉您如何修复错误。您可以在此处找到 gcc 与 clang 错误输出的比较:http: //clang.llvm.org/diagnostics.html。
Otherwise, as noted by Matteo Italia, look to STLFilt.
否则,正如 Matteo Italia 所指出的,请查看STLFilt。
For the color aspect, Edwin Buck's solution (colorgcc) is very good. You can create symlinks to it named gcc, g++, etc so that it always gets used whenever you run a compiler command (provided you setup the paths in the colorgcc script correctly). I find this especially convenient when compiling some large project from a Makefile. Helps tremendously to detect where exactly something went wrong (especially when you do parallel compilations with make -j
).
对于颜色方面,Edwin Buck 的解决方案(colorgcc)非常好。您可以创建名为 gcc、g++ 等的符号链接,以便在您运行编译器命令时始终使用它(前提是您在 colorgcc 脚本中正确设置了路径)。从 Makefile 编译一些大型项目时,我发现这特别方便。极大地帮助检测出问题的确切位置(尤其是当您使用 进行并行编译时make -j
)。
回答by David C. Bishop
GCC as of 4.9 has colourized output ?
从 4.9 开始的 GCC 有彩色输出吗?
Append the following to your bashrc/zshrc/whatever.
将以下内容附加到您的 bashrc/zshrc/whatever 中。
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
回答by Edwin Buck
回答by Paul Fultz II
I improve it by using sed to color the output with this script:
我通过使用 sed 使用此脚本为输出着色来改进它:
#!/bin/bash
while read x ; do echo $x ; done \
| sed -e "s/.*error:.*/\x1b[1;36m&\x1b[0m/" \
-e "s/.*warning:.*/\x1b[1;36m&\x1b[0m/" \
-e "s/^\(.*\)\(required from\)/\x1b[1;36m\x1b[0mnote: /" \
-e "s/^\(.*\)\(In instantiation of\)/\x1b[1;36m\x1b[0mnote: /" \
-e "s/^\(.*\)\(In member\)/\x1b[1;36m\x1b[0mnote: /" \
| sed -e "s/error:/\x1b[1;31m&\x1b[1;36m/" \
-e "s/warning:/\x1b[1;35m&\x1b[1;36m/" \
-e "s/note:/\x1b[1;30m&\x1b[0m/"
回答by Benno Straub
If you use gcc 4.9 or higher there are a couple built-in options:
如果您使用 gcc 4.9 或更高版本,则有几个内置选项:
- -fmessage-length=n, which optimizes the output for lines of n characters length.
- -fdiagnostics-color=always, which applies some nice colors.
(Works fine on linux, not so much on msys)
- -fmessage-length=n,它优化了 n 个字符长度的行的输出。
- -fdiagnostics-color=always,它应用了一些漂亮的颜色。
(在 linux 上运行良好,在 msys 上没有那么多)
For more options and exact usage heres the documentation:
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html
有关更多选项和确切用法,请参阅文档:https:
//gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html
回答by Matteo Italia
When dealing with the STL1, STLFiltcomes to the rescue.
在处理 STL 1 时,STLFilt来救援。
On a related note, I also heard that the clang compiler produces by itself much better error messages in general.
在相关说明中,我还听说 clang 编译器本身通常会产生更好的错误消息。
Nitpickers' corner挑剔者的角落
- Actually, not the SGI STL, but the parts of it that have been adopted in the C++ standard and are part of the standard library.
- 实际上,不是 SGI STL,而是 C++ 标准中采用的部分,并且是标准库的一部分。
回答by Mark B
I can't answer your question directly, but note that in regards to templates if you look at the top message (the error caused by the templated) and the bottom "instantiated from here" message, it usually becomes much more clear. Ignore all the middle messages as they are probably not going to help.
我无法直接回答您的问题,但请注意,关于模板,如果您查看顶部消息(由模板化引起的错误)和底部“从这里实例化”消息,通常会变得更加清晰。忽略所有中间消息,因为它们可能无济于事。
回答by scavenger
there is my personal function:
这是我的个人职能:
colorgcc()
{
perl -wln -M'Term::ANSIColor' -e '
m/not found$/ and print "\e[1;30m$`\e[0m", "$&", "\e[0m"
or
m/found$/ and print "\e[1;30m$`\e[0;32m", "$&", "\e[0m"
or
m/yes$/ and print "\e[1;30m$`\e[0;32m", "$&", "\e[0m"
or
m/no$/ and print "\e[1;30m$`\e[0m", "$&", "\e[0m"
or
m/ Error |error:/i and print "\e[1;91m", "$_", "\e[0m"
or
m/ Warning |warning:/i and print "\e[0;33m", "$_", "\e[0m"
or
m/nsinstall / and print "\e[0;36m", "$_", "\e[0m"
or
m/Linking |link: |\.a\b/ and print "\e[1;36m", "$_", "\e[0m"
or
m/Building|gcc|g++|\bCC\b|\bcc\b/ and print "\e[1;30m", "$_", "\e[0m"
or
print; '
}
回答by Gilson
you can use GilCCwhich is a Ruby tool that will convert GCC output to color in real-time. It is free and you can customize it to your preference. It is not intrusive as it does not change your gcc setup and you don't have to edit any system files such as .bash. You have to install Ruby and be able to invoke Ruby programs from the command line. Right now you have two options Perl script or GilCCand if you work with Ruby you will like GilCC.
您可以使用GilCC,它是一个 Ruby 工具,可以将 GCC 输出实时转换为颜色。它是免费的,您可以根据自己的喜好对其进行自定义。它不是侵入性的,因为它不会更改您的 gcc 设置,并且您不必编辑任何系统文件,例如 .bash。您必须安装 Ruby 并能够从命令行调用 Ruby 程序。现在您有两个选项 Perl 脚本或GilCC,如果您使用 Ruby,您会喜欢 GilCC。
Then whenever you call "GillCC" it will call "make" behind the scenes with your parameters such as "clean", "debug" or "release". for example if normally you call: "make debug" you should call "GilCC debug".
然后,每当您调用“GillCC”时,它都会在幕后使用您的参数(例如“clean”、“debug”或“release”)调用“make”。例如,如果您通常调用:“make debug”,则应调用“GilCC debug”。
Almost forgot to mention that GilCChas some statistics such as # of warnings and error as well as build time. These things are handy when you are trying to improve your build.
差点忘了说,GilCC有一些统计数据,例如警告和错误的数量以及构建时间。当您尝试改进构建时,这些东西非常方便。
The link to the download page is here.
下载页面的链接在这里。