How to colorize gcc output using colorgcc

GCC 4.9 and later versions have built-in support for color in its output. For more info about using this, see this post.

If you are using older versions of GCC, then an option to colorize gcc output is colorgcc.

  • It can be installed easily:
$ sudo apt install colorgcc

This installs a Perl script at /usr/bin/colorgcc.

  • Create symbolic links in a directory that is in your PATH for each of the compilers you use. For example:
$ ln -s /usr/bin/c++ /home/joe/color-c++
  • Now you can invoke Make like this:
$ make CXX=color-c++

Note that the above trick will not work if your Makefile was generated by CMake.

  • To ask CMake to use colorgcc, set the CMAKE_CXX_COMPILER to the path of the symbolic link:
set(CMAKE_CXX_COMPILER /home/joe/color-c++)

Tried with: ColorGCC 1.3.2 and Ubuntu 16.04

2 thoughts on “How to colorize gcc output using colorgcc

  1. It’s also worth knowing about the environment variables used by CMake to decide on the compiler. If you have one that you want to commit to permanently, put it in your dotfiles somewhere (.bashrc, .zshrc, wherever really). CMAKE_C_COMPILER is set by the CC environment variable, and CMAKE_CXX_COMPILER by CXX. There’s also FC for fortran and I think F90 as well.

    Suppose you wanted to use GCC for a certain project:

    export CC=gcc
    export CXX=g++
    mkdir build && cd build && cmake ..

    If you wanted to use clang

    export CC=clang
    export CXX=clang++

    So if you have the older GCC and wanted colorized output (after creating the symlink):

    export CC=/home/joe/color-c++
    export CXX=/home/joe/color-c++

    Note: I forget if export works for all shells or if that’s just bash. Perhaps setenv is more safe?


    1. Stephen: Your comment was very insightful. I had no idea about compiler environment variables and that CMake was picking them up. Thanks!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.