49

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?

It may sound silly to ask this question here, but I've been unable to google my way out of this.

salezica
  • 74,081
  • 25
  • 105
  • 166

10 Answers10

44

GCC as of 4.9 has colourized output ☺

Append the following to your bashrc/zshrc/whatever.

export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
David C. Bishop
  • 6,437
  • 3
  • 28
  • 22
  • 7
    and compile with `-fdiagnostics-color` or `-fdiagnostics-color=ALWAYS` if you're running through a piped build system. – Qix - MONICA WAS MISTREATED Jul 07 '14 at 04:19
  • I think Qix capitalized `ALWAYS` for emphesis, but readers should note that it should be `-fdiagnostics-color=always` or you might get an error saying that ALWAYS is an unrecognized argument and that valid arguments are `always`, `auto`, and `never`. – drwatsoncode Feb 24 '19 at 01:28
  • Doesn't seem to work for me for g++ (compiling through ninja build system) – Caleb Stanford Jun 26 '20 at 12:55
39

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.

Otherwise, as noted by Matteo Italia, look to 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).

AVH
  • 11,349
  • 4
  • 34
  • 43
  • 2
    It's been quite a long time since I asked this question and took your answer. I stumbled upon it while cleaning bookmarks and realized I never commented on the issue. For future reference, I did try clang++ and was most pleasantly surprised by the much cleaner compiler output :) thanks! – salezica Feb 15 '12 at 20:48
  • 1
    For the color aspect, I find [colout](http://nojhan.github.com/colout/) easier to use and it's g++ theme better looking than colorgcc. It also do syntax coloring within the error messages and its text stream approach permits to color output mixing several commands (like g++ called from a CMake's makefile and filtered with grep). – nojhan Apr 04 '13 at 13:52
  • Thanks for this. As a bonus, clang colorizes it's output by default (assuming you set the `CXX=clang` in your makefile). Me happy. – Daniel Hanrahan Aug 04 '13 at 20:50
12

There's colorgcc, a perl script which wraps the gcc (g++) output with color for easier readability.

As far as the "output" of gcc (g++) I am guessing you're not complaining about the compiled binaries :)

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
11

I improve it by using sed to color the output with this script:

#!/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\1\x1b[0mnote: \2/" \
-e "s/^\(.*\)\(In instantiation of\)/\x1b[1;36m\1\x1b[0mnote: \2/" \
-e "s/^\(.*\)\(In member\)/\x1b[1;36m\1\x1b[0mnote: \2/" \
| 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/"
Paul Fultz II
  • 17,682
  • 13
  • 62
  • 59
6

If you use gcc 4.9 or higher there are a couple built-in options:

  • -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)

For more options and exact usage heres the documentation:
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html

Benno Straub
  • 2,268
  • 3
  • 19
  • 21
4

When dealing with the STL1, STLFilt comes to the rescue.

On a related note, I also heard that the clang compiler produces by itself much better error messages in general.


Nitpickers' corner
  1. 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.
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
2

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

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; '
}
scavenger
  • 11
  • 1
1

There is colorgcc.

js.
  • 1,787
  • 19
  • 22
0

you can use GilCC which 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 GilCC and if you work with Ruby you will like 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".

Almost forgot to mention that GilCC has 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.

The link to the download page is here.

Gilson
  • 1,708
  • 3
  • 16
  • 21