Compiling Unix-ish Code on Mac OS X

by Brian Jepson

Note: This article assumes you are comfortable using the Mac OS X Terminal and have some familiarity with downloading and extracting Unix packages.

Some of you may have installed the packages on the development CD-ROM that comes with Mac OS X. This CD includes, among many other things, GNU C and a whole bunch of compilation tools. Because Mac OS X is based on Darwin, an open source Unix in the BSD family, you can install a lot of the programs that Linux and BSD users have been enjoying. However, there are a couple of things to watch out for.

When you first download and extract some source code, you might be tempted to edit the Makefile and just run make. Or, you might not know what to do at all. The first thing you should do is cd to the source code directory and read the INSTALL and README files (the name of the source code directory is usually the name of the package with a version number, such as foo-1.2.3).


Most open source packages include a configure script that determines what type of Unix you're using and creates whatever Makefiles are needed to compile the application. To invoke this script, type ./configure, and press enter. In some cases, this script might fail. If so, this hint might help out.
Even if that hint works, it might not always lead to a successful compilation. For example, I was able to get lynx to configure on my system, but I got a vicious "signal 10" error during compilation. I soon learned that the most stable version of Lynx does not support Mac OS X out of the box, but the latest development version does. So, I downloaded that version, and it works fine on my machine!

The configure script is created by the autoconf package, a powerful aid in developing code that compiles on a wide range of Unix-like operating systems.


After you configure the source code, run the make command (unless otherwise directed by the INSTALL or README file). This will compile the code. If the compilation succeeds, you should run any tests that were supplied. These can usually be run with the command make test or make check. An error in the tests indicates that something may have gone wrong with the compilation. If this happens, you should review the documentation that comes with the source code for instructions on reporting or correcting the error.

Installing it

If all went well, you can install the program. This is usually accomplished by the command make install, but please review the README and INSTALL files first. You may need to use the sudo command to assert your administrator privileges, as in sudo make install. When you use sudo, you'll be asked for your password. Please note that this gives you total control over the system - if you're installing a package that is poorly configured or contains malicious code, you could end up overwriting important system files and damaging your system.

If I'm installing unfamiliar software, I usually run make install without sudo and watch what it tries to do. In most cases, it will copy some files into the /usr/local directory somewhere. For the most part, this is safe, since Mac OS X does not place any critical files in that folder. If you decide to do this dry run, you'll get a lot of error messages as you do it, since your user account doesn't have the privileges to create files in /usr/local.

Another alternative to the dry run is to give yourself permission to write files in the /usr/local directory. This way, you'll never need to use sudo to install software, so you'll never put your system at any major risk (of course, installing software from an untrusted third party is always a risk). To do this, cd to the /usr directory, and run the command sudo chown -R USERNAME local, replacing USERNAME with the name of the user you log in as (check the value of the $USER environment variable). This command makes you the owner of the local directory and anything contained in it. Other users should be able to execute programs in there, but only you will be able to make changes.

Running the application

If this is the first time you've installed something, chances are you haven't made an important change to your login files that allows you to execute programs in the /usr/local/bin directory. Unix uses a $PATH environment variable to figure out where to locate binaries. If you add the following line to your $HOME/.cshrc file, you'll have the /usr/local/bin directory in your path:

setenv PATH /usr/local/bin:$PATH

After you add that line to your .cshrc file, close all the currently open Terminals, and open them again. You'll have /usr/local/bin in your $PATH, and you'll be able to run programs by typing their name at the shell prompt, as in:

[localhost:~] bjepson% lynx