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...)
-
2Also stackoverflow.com/questions/1032237/… ; (try clang too, colors output all by itself and has great diagnostics) – Mat Feb 17 '13 at 15:59
-
stackoverflow.com/a/49064031/4561887 – Gabriel Staples Mar 2 '18 at 7:04
-
Recent GCC are able of color messages – Basile Starynkevitch Mar 2 '18 at 7:33
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
-
2Just want to say this is fantastic and works like a charm, however as it's currently typed I got an error about "nested quantifiers in regex" and fixed by adding '\' to the '+' in "g++" and it worked great after that. – Mike Dannyboy Jan 14 '15 at 18:25
-
What does it mean "Just alias your make to this script and make sure it's executable..."? How should I use this script? – Mateusz Piotrowski Jun 13 '15 at 18:04
-
1I believe he means to add an alias to your bashrc file such as <alias make='make-thisscript'> So that when you type in <make> the <make-thisscript> will actually run instead. This script then calls the actual make. Search for "setting permanent alias" for more information. – Kurt E. Clothier Jun 17 '15 at 15:52
-
For me, error information, warnings, and successful compiling are all in green. Does anybody know why? – narengi Jul 29 '15 at 20:55
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).
-
4Just for sake completeness, one needs to pass
-fdiagnostics-color=always
in CFLAGS. – Pouya Jun 26 '14 at 8:29
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:
-
1Looks interesting, but the instructions for installation don't work on ubuntu. I tried the "easy" pip command, which fails because several python packages were missing. Some of them I could find as ubuntu package, but not all (for example exitstatus). After running pip as root it installed something but afterwards I had only stuff in /usr/local/lib/python2.7/dist-packages/outproc (no any of the directories the installation instruction mention), and no 'bin' dir anywhere, no 'outproc' binary anywhere... – Carlo Wood May 29 '17 at 22:02
-
@CarloWood, this is a Python 3 package. I've just tried in a minimal Ubuntu 16.04 docker container:
apt-get install -y python3-{pip,setuptools,wheel}
,pip3 install -U pip
and finallypip install outproc
-- everything has installed w/o problems. – zaufi May 31 '17 at 5:06 -
Ok, with those instructions I am able to get something to install (assuming the last two commands have to be run as root, too). But after that, I don't have
/usr/lib/outproc/bin
or/usr/bin/outproc
that your webpage talks about that I'd have to symlink. I have no idea how to test this installation therefore. I found an 'outproc' in/usr/local/bin
with the current date (so I assume that was just installed), but running it gives errors:outproc -m g++ ... File "/usr/local/bin/outproc", line 11, in <module> ... sys.exit(main()) ...
– Carlo Wood May 31 '17 at 23:14 -
File "/usr/local/lib/python3.5/dist-packages/outproc/cli.py", line 236, in main ... return a.run() ... File "/usr/local/lib/python3.5/dist-packages/outproc/cli.py", line 182, in run ... log.eerror('Pipe mode not implemented') ... File "/usr/local/lib/python3.5/dist-packages/outproc/logger.py", line 33, in eerror ... print(' \x1b[0;31;1m*\x1b[0m {}'.format(msg), file=sys.stderr) ... NameError: name 'sys' is not defined
– Carlo Wood May 31 '17 at 23:15 -
@CarloWood, thanks for your patient,
import
error I've already fixed in version 0.18, the docs would be actualized – zaufi Jun 1 '17 at 19:35
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!
- Go here and click "Clone or download" --> "Download Zip". I saved it into "~/Downloads/Install_Files"
- 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".
- 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
- Make it executable:
sudo chmod +x /usr/bin/colorgcc
- Make the "~/bin" directory if it does not yet exist:
mkdir ~/bin
- *Make symbolic links that point to "/usr/bin/colorgcc" so that whenever you call
gcc
org++
it automatically callscolorgcc
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)
- 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
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.
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.