I have a confession to make: I’m a bit of a compiler hoarder. As I write this, the CentOS-7 virtual machine on my workstation at home has eleven versions of GCC and ten versions of Clang installed. I like to have multiple versions of GCC and Clang available on my various Linux-based home and work systems so that I can easily compare old and new behavior, check for performance improvements and bug fixes, explore new language features, and retain the stability of previous compiler releases that have proven themselves.
However, there’s often a practical problem when trying to do this. The standard compiler installations blessed and distributed by the major Linux distros typically install into the system directories
/usr/lib. That’s fine if you only need one version of GCC and/or Clang, but it doesn’t work so well when you want multiple versions. Upgrade packages usually replace the existing compiler, which is not the behavior we want. Packages that attempt to install additional new versions can sometimes overwrite the files of an existing installation, resulting in a mis-configuration of one or more versions.
There are also cases where it’s impermissible or undesirable to add or change files in the system directories. Company policy may prohibit you from installing software there (although this seems less and less likely in modern software development shops). Or perhaps you need to develop on a system that is configured as closely as possible to production systems, and so system directory changes are discouraged.
In any event, it would be great if there was a way to install an arbitrary number of compilers on your system independently of each other, and with no changes to the standard system directories. It’s possible, but does require a little work. In short: you must build it yourself! As it turns out, GCC and Clang are remarkably flexible in how they can be configured and built, and we’re going to use that flexibility to our advantage.
In this post I’m going to describe a set of scripts I wrote for building GCC such that each installed version is completely independent of every other installed version, allowing you add or remove them at will without affecting the system directories.