Is there an easy way to COLOR-CODE the compiler outputs?

此生再无相见时 提交于 2019-12-09 04:03:39

问题


gcc (or other compilers) often generate huge text output and it's very difficult to see where the error is or miss warnings. I've done some search but havn't found a clean simple solution to color code the compiler output (so for instance warnings are yellow, errors are red, etc...)


回答1:


here's an alternative if you are looking for something very simple:

#!/bin/bash -e

make ${@} 2>&1 | perl -wln -M'Term::ANSIColor' -e '
m/Building|gcc|g++|\bCC\b|\bcc\b/ and print "\e[1;32m", "$_", "\e[0m"
or
m/Error/i and print "\e[1;91m", "$_", "\e[0m"
or
m/Warning/i and print "\e[1;93m", "$_", "\e[0m"
or
m/Linking|\.a\b/ and print "\e[1;36m", "$_", "\e[0m"
or
print; '

Just alias your make to this script and make sure it's executable...




回答2:


Gcc 4.9 seems to have added this feature via the -fdiagnostics-color flag:




回答3:


Debian and Ubuntu gives the colorgcc package for that purpose.

And I usually run gcc (and make) thru emacs with M-x compile then the messages are colorized.

addenda

GCC 4.9 has a native colorization facility and GCC 6 - released end of April 2016 - (and probably GCC 5 too) is enabling it by default (when stdout is a terminal).




回答4:


Ok, I'll just leave a notice about my own (python based) tool also :)

It is called Pluggable Output Processor and designed not only to colorize output of one particular program. Here is sample GCC output before:

After:




回答5:


See colorgcc, a perl script that coulours the gcc output.




回答6:


ColorMake will colorize output of GCC and Make, highlighting warnings and errors.




回答7:


How to install and use colorgcc to colorize your gcc compiler output:

At least 3 answers here so far mention colorgcc, but NONE OF THEM EXPLAIN HOW TO INSTALL IT! (And it's not obvious). So, here's how to install the latest version in Ubuntu!

  1. Go here and click "Clone or download" --> "Download Zip". I saved it into "~/Downloads/Install_Files"
  2. Navigate to it in your file browser and right click it and go to "Extract Here." I now have a directory called "~/Downloads/Install_Files/colorgcc-master".
  3. Copy the "colorgcc.pl" script to "/usr/bin/colorgcc" to "install" it (be sure to use the correct directory according to where you extracted it above): sudo cp ~/Downloads/Install_Files/colorgcc-master/colorgcc.pl /usr/bin/colorgcc
  4. Make it executable: sudo chmod +x /usr/bin/colorgcc
  5. Make the "~/bin" directory if it does not yet exist: mkdir ~/bin
  6. *Make symbolic links that point to "/usr/bin/colorgcc" so that whenever you call gcc or g++ it automatically calls colorgcc instead:
    • ln -s /usr/bin/colorgcc ~/bin/g++
    • ln -s /usr/bin/colorgcc ~/bin/gcc
    • (if you ever want to uninstall colorgcc for some reason just delete these symbolic links "~/bin/g++" and "~/bin/gcc", and the Perl script: "/usr/bin/colorgcc" and you're done)
  7. Done!

Here is a sample g++ output now when I call g++ -Wall -std=c++11 time_until_overflow_2.cpp -o time_until_overflow_2:

*Note: making these symbolic links in "~/bin" only works if "~/bin" is in your PATH variable in a location before the folder where the actual gcc and g++ executables are located. To ensure you have "~/bin" in your path you can view the PATH variable contents with: echo $PATH. If you don't see "/home/YOUR_USERNAME/bin" at the beginning of your path, add it with: export PATH=~/bin:$PATH.

References:

See here for more info. and for where I originally learned most of these steps: https://imranfanaswala.wordpress.com/2009/02/02/setting-up-colorgcc/. Thanks Imran Fanaswala!

~GS




回答8:


you can use GilCC which is a Ruby tool that will convert GCC output to color in real-time. Right now you have two choices: Perl script (colorGCC) or GilCC and if you already work with Ruby you will like GilCC.

Unique to GilCC; GilCC has warning and errors counters and also shows compile time, very handy when you are trying to improve things. Because it is in Ruby it is cross platform. It is flexible and you can add more gems to customize it anyway you want.

The link to the download page is here.

https://github.com/gilmotta/GilCC




回答9:


Although GCC 4.9 has -fdiagnostics-color option to enable colored outputs to terminals, I have created a tiny tool called 'crror' to get colorized compiler output.

It supports outputs from make as well. I can add colorize patterns for other tools if anyone requires.



来源:https://stackoverflow.com/questions/14922960/is-there-an-easy-way-to-color-code-the-compiler-outputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!