As part of a little project to make detecting memory/pointer errors easier for beginning C/C++ programmers, I’ve installed a number of different compilers on my system. I wanted to make sure that my approach was widely applicable.
At this point, there are 4 (3.5 really) major C/C++ compilers available for MacOS X 10.7. What follows is a brief description of each, and some background as to how we got here.
The C/C++ compiler situation on Macs has always been in flux. In the early days when MacOS ran on Motorola 68K processors, the main tool was Symantec’s Think C/C++ compiler. When Apple switched to PowerPC processors beginning in 1994, neither Symantec nor Apple had a good option available, so Metrowerks’ CodeWarrior became the compiler of choice for Mac development. Until OS X arrived, there were a few other less well known tools (Apple’s MPW/MrC) but CodeWarrior was what all the major Mac applications were built with, from Mozilla to Photoshop.
The system compiler for MacOS X from the start was a heavily customized version of the GNU C Compiler (GCC), owing to OS X’s NextSTEP/OpenSTEP roots. This version evolved largely in parallel with the standard version of gcc distributed by the Free Software Foundation, although some cross-pollination occurred. Initially, most applications were still built with CodeWarrior, but Apple gradually encouraged people to switch to their tools and Metrowerks eventually discontinued their desktop IDE/compilers right around the time Apple transitioned to Intel x86 processors (they were bought out by Motorola/FreeScale and technically still provide some embedded systems development tools).
However, at the same time as Apple’s switch to Intel processors, they also began to mover over to the LLVM compiler framework. Originally a research project at the University of Illinois, LLVM offered Apple the promise of improved compile-time (always a sore spot with gcc) and greater extensibility (the ability to easily add plugins like additional language front-ends and IDE integration, etc.). LLVM borrowed the GCC front-end initially, offering good source-level compatibility with GCC for C/C++ and Objective C. The other advantage for Apple of LLVM was control over the code – the source was licensed under a non-GPL open-source license, and changes did require external approval. The lead LLVM developer was hired by Apple in 2005.
When MacOS X 10.5 Leopard shipped, Apple’s Xcode 3.1 included 2 compilers – the old Apple-modified GCC 4.2 and the new LLVM with GCC 4.2 front-end. Matters were further complicated as the main GCC source tree was placed under the GPL (GNU Public License) v3 license in 2007, starting with the 4.2.2 release. Apple was unhappy with the new licensing scheme, believing it restricted their options (the GPL v3 was designed to make locked systems, incompatible with the GPL). As a result, LLVM’s main front-end has remained based on GCC 4.2.1 since that point, while Apple has sought to replace it with the new Clang C/C++ front-end. As Clang was developed specifically with LLVM in mind, it offered the advantage (over the existing GCC 4.2 front-end) of improved compiler performance and a dramatically smaller and simpler code-base.
Clang was released alongside MacOS X 10.6, which came with no less than 3 compilers – GCC 4.2 (Apple modified), LLVM-GCC, and Clang. With 10.7 Lion, Apple has dropped their version of GCC 4.2 entirely. LLVM-GCC and Clang are both present, but Apple has made clear that LLVM-GCC will be going away just as soon as Clang’s C++ support is ready for prime-time.
So as of MacOS X 10.7 Lion, Apple’s Xcode includes 2 compilers – Clang and LLVM-GCC. There are also versions of the commercial Intel C++ compiler, the standard Free Software Foundation version of GCC, and a small research project based on the original BSD Unix compiler pcc (Portable C Compiler) which are available.
Clang – Currently the Apple-blessed compiler for OS X, Clang offers excellent Xcode integration, helpful error messages, fast compile times, and pretty decent code. The main downside is that it still has a few more bugs compared to more mature options. Clang can also be built on Linux and many other platforms. It requires LLVM as it’s back-end. The current version is 3.0.
LLVM-GCC – Kept around by Apple for compatibility reasons, LLVM-GCC probably won’t be around much longer. It does have the advantage of offering a more mature front-end than Clang, while still giving access to the LLVM optimizers. It will also work on Linux and on most other UNIX platforms. The current version is 3.0 (the front-end is 4.2).
(FSF) GCC – The official FSF-sanctioned GNU Compiler Collection is no longer supported by Apple, meaning you have to build and install it yourself. It offers the most mature front-end of the open-source options, and in most benchmarks it generates the best code. On the flip side, it is generally slower to compile than the others, and has some of the less helpful error messages. It can be built on just about any platform – Linux, UNIX and Windows. The current version is 4.6.
Intel C/C++ – Intel’s compilers on MacOS X are a straight-off port of their Linux tools. It’s a commercial tool, but they do have a 30-day demo available for download. Naturally, performance is heavily tuned for the latest Intel processors. On the whole, it seems to produce slightly better code than GCC, although for specific computational tests, it may have more significant advantages (automatic vectorization support for example is quite good). The compiler uses the EDG front-end, and offers the same language extensions that GCC does. It is also available for Linux and Windows. The current version is 12.0 (confusingly labeled 2011).
PCC – Based on the original BSD C compiler, the Portable C Compilerß is small, reasonably fast, and can build most simple programs. It doesn’t have the many language extensions that GCC or the others do, and it doesn’t do C++. It can be built on most UNIX platforms. The current version is 1.0.
Which do I use?
For the time being, I’m using both Clang and FSF GCC. Clang has been the easiest for development, but GCC has helped me catch some nasty bugs.