Building Clang on Linux

GCC is not the only open source C++ compiler that’s available out there on the interwebs — there’s also Clang, which is part of the LLVM Compiler Infrastructure. Each has its strengths and weaknesses, and both are excellent compilers for a wide variety of platforms. Whenever possible, I try to compile the Linux code I write with both compilers, for the simple reason that sometimes one will catch an error or emit a warning that the other one missed. I find that having my code build cleanly with at least two compilers and all warnings enabled is a useful best practice for improving code quality.

As it turns out, Clang on Linux has a very interesting capability: it can use GCC’s version of the standard C++ library (libstdc++), or its own version (libc++). The choice of standard library may be specified using a command-line argument (-stdlib=libstdc++ or -stdlib=libc++, respectively). Furthermore, if you have multiple versions of GCC installed on your Linux system, you can provide an optional command-line argument to Clang that specifies the GCC version whose libstdc++ you want to use (--gcc-toolchain=<path>).

This post is a followup to Building GCC on Linux, and in it I’m going to describe a set of scripts I wrote for building Clang on Linux. I call this collection clang-builder and it is available on GitHub. (continue…)

Building GCC on Linux

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/bin and /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. (continue…)