Compiler output diagnostics can be easier to read when errors, warnings, and notes are displayed in different colors. Whether color is enabled by default depends on how compiler itself was built and packaged by the operating system or toolchain distributor. Many modern distributions enable colored diagnostics automatically in interactive terminals, while others leave them disabled. This tutorial explains how to enable or disable color for gcc or g++ compiler output.
Create a source file that produces a compilation error:
printf "int main() {\n return y;\n}" > main.c
The gcc is used for demonstration, but the same options and environment variable work with g++ when compiling C++ programs.
Enable color output
To always display colored diagnostics output, use the -fdiagnostics-color=always option:
gcc -fdiagnostics-color=always main.c -o test
The shorter -fdiagnostics-color form is an alias for -fdiagnostics-color=always:
gcc -fdiagnostics-color main.c -o test
Disable color output
Colorized diagnostics can improve readability in a terminal, but plain text is often more suitable when compiler output is redirected to a file, processed by scripts, or viewed in environments that do not support ANSI escape sequences.
To disable colored diagnostics, use the -fdiagnostics-color=never option:
gcc -fdiagnostics-color=never main.c -o test
The -fno-diagnostics-color option is a convenient alias for -fdiagnostics-color=never:
gcc -fno-diagnostics-color main.c -o test
Color output can also be disabled through the GCC_COLORS environment variable by assigning it an empty value for a single command:
GCC_COLORS='' gcc main.c -o test
Alternatively, export the variable to affect all subsequent GCC commands in the current shell session:
export GCC_COLORS=''
gcc main.c -o test
Leave a Comment
Cancel reply