Gcc 4.9 colors and sudo
Since version 4.9, the gcc compiler is finally able to shows a colored output, one of the many good clang’s features. This is so helpful to locate warnings and errors that you quickly become addicted to it. Unfortunately, for some questionable reasons, this feature is turned off by default. In order to use it you have to either define the environment variable GCC_COLORS
(e.g. by export GCC_COLORS=1
) or use the flag -fdiagnostics-color=auto
.
The problem is: when you sudo make install
a CMake-generated Makefile, you get no colors. That’s because, for security reasons, sudo
resets the environment and thus GCC_COLORS
is not defined anymore. In particular this is due to the line Defaults env_reset
in the file /etc/sudoers
.
While I was looking for a fix to this problem, I realized that there not exists a clean solution, at least to my likings.
What I have done, at the end, is adding this line to /etc/sudoers
(you have to use sudo visudo
):
While this is an hacky solution, is the least hacky for my tastes, until I find something better. Other possible solutions, which I discarded but may be useful to others, are the following:
-
Define the
GCC_COLORS
variable in/etc/profile.d/
or/etc/bash.bashrc
or/etc/profile
. This works only usingsudo -i
orsudo su -
instead ofsudo
and I don’t want to. -
Use
sudo -E
instead ofsudo
. This preserves the whole environment and can be a security mess. -
Use
sudo env "GCC_COLORS=$GCC_COLORS" make install
. But obviously this is annoying and alias for it would be ugly. -
Add always the flag
-fdiagnostics-color=auto
. This is even more annoying.
I’m sorry, but I have to say that the only sane solution would be enabling by default the colors, like clang does.