4

gcc4.9 supports the colorizing diagnostics for compiler warning/error messages.

We can enable it for a particular program using the option "fdiagnostics-color". Currently I am using gcc4.9.1 and I append this particular option in my makefile as follows:

CC = /home/mantosh/gcc-4.9.1/bin/g++ -std=c++1y -Wall -pthread
DFLAG = -g -gdwarf-2 -fdiagnostics-color=always
OUTFILE = test

$(OUTFILE): test.cpp
    $(CC) $(DFLAG) -o $(OUTFILE) test.cpp

clean: 
    rm -f *.o $(OUTFILE)

If I compile a *.cpp file a get the following nice coloured message. This is really great feature added by GCC.

enter image description here

While reading the GCC offical link, it seems that this setting can permanently enabled using the GCC environmental variable "GCC_COLORS".

Could somebody explains how to set/change/customize this particular environment variable?

I am using Ubuntu12.04/GCC4.9.1.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48

1 Answers1

7

just add this line to your ~/.bashrc file:

export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

You can then reload it with source ~/.bashrc so you don't have to logout/login.

mcarton
  • 27,633
  • 5
  • 85
  • 95
gengisdave
  • 1,969
  • 5
  • 21
  • 22
  • 1
    the way I read it setting the `GCC_COLORS` variable only changes the colors, but setting it will not set `-fdiagnostics-color=always` which would be desirable on e.g. a CI server (https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Language-Independent-Options.html) – TBieniek Sep 24 '15 at 09:33
  • setting `GCC_COLORS` to a non-empty string implies `-fdiagnostic-color=auto`, this works when `stderr` is on the terminal, otherwise you must pass `-fdiagnostic-color` to the compiler; the colors string I wrote is the default one used by gcc – gengisdave Sep 24 '15 at 09:40
  • @gengisdave This is sometimes not possible, especially on a CI server that launch build tools like `rpmbuild` that reset everything. It could be nice to be able to enable it globally. – Adrien Clerc Sep 20 '17 at 14:11