OS X Lion for UNIX Geeks: Installing the C Compilers

The first thing to know is good news. You don’t have to pay US$99 and become a member of Apple’s Developer program to follow these instructions. You can download Apple’s Xcode 4.1 for free, and its installer will give you gcc, g++, and llvm-gcc nicely installed and ready for the command line. The bad news is that you’ll have to be using Lion. And, you’ll need to have an Apple ID, even though the download is free.

What starts this odyssey for most technical users is that they’re perusing a new Mac, open a terminal, and type, out of curiosity:

gcc -v

and find to their dismay that that there’s no C compiler to be found. WTF?

gcc1

The Ugly “Before”

Unlike most other UNIX vendors, Apple feels that this is an unnecessary addition and that technical users, if they need it, can go install it. However, those who are new to the Mac are often not quite sure about the best way to do that. Here’s the easy way.

________

1. First, go to the Apple menu in Lion and select “App Store…” Do a search for “Xcode.” You should see something similar to this. (Note, in my case, Xcode is marked as already installed.)

MAS

Mac Apple Store (MAS) Xcode on the left.

2. After you download Xcode, there will be an installer in /Applications. It looks like this in icon view.

Xcode installer

 

Xcode Installer icon

3. Double click this application, if it didn’t already launch after the download, and a complete development system will be installed on your Mac and placed in the root of the file system — in a directory called “Developer.” You can explore the contents and the Integrated Development Environment (IDE) at your leisure. So just go ahead and quit Xcode (CMD-Q) after the installer is done.

Welcome to Xcode

You won’t be needing this right away, so quit the app.

4. Now open the terminal and type “gcc -v” again. Bliss!

gcc-after

The Beautiful “After”

You’ll notice that gcc 4.2.1 is installed. Also, g++ is installed. For those who prefer, the more modern Low Level Virtual Machine (LLVM) C compiler that Apple is now using is also installed. These are all in the expected place: /usr/bin. Note that the Xcode installer also drops in the man pages for these compilers.

llvm pointers

llvm pointers in /usr/bin

Additional Notes

That’s all you need to do. If at some future point, you want to explore GUI app development on the Mac, you can learn Xcode, found in /Developer/Applications as well as the primary development language for OS X and iOS apps called Objective-C. (The version of gcc installed will compile Objective-C code.)

To learn more about how Apple has implemented llvm-gcc, the default compiler in Xcode, here’s a really good, quick and handy reference article to digest before you dig into Apple’s extensive developer documentation.

Apple is no longer maintaining gcc, so you might as well move to llvm-gcc on the command line.

Finally, the sake of completeness, I should add that Dr. Gaurav Khanna, an astrophysicist in the Physics Department at the University of Massachusetts at Dartmouth, maintains a computational tools page that goes well beyond the simple C compilers mentioned above. He generally keeps these tools updated for the latest version of OS X.

________

UPDATE: April 4, 2012. Things have changed slightly since this article was written. See this update: “Install the Command Line C Compilers in OS X Lion.”

Leave a Reply

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