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

Advertisements

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?

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s