Compiling Matlab Mex Files on a Mac

So, I’ve finally received my super-hot new 15” MacBook Pro, and I’m super happy with it! Although slightly reluctant about upgrading to Lion and Xcode 4, I thought this would probably be the best moment to do it. I heard some bad stories about backwards compatibility issues of Lion, but heck, they’ve had plenty of time to sort that out, right? Wrong.

One of the things I need for my day-to-day research is the Matlab version of LibSVM. I use the version provided by UC Berkeley, which is based on LibSVM 3.0-1 but includes the Histogram Intersection Kernel, useful for my work with LBPs and other histogram-based appearance descriptors. Note that LibSVM itself is now at version 3.11, but I haven’t been able to find out if there are any significant changes between 3.0-1 and 3.11 that would warrant me changing over.

Anyway, after downloading and unpacking the LibSVM files, I tried to set up my fresh Matlab installation to use the correct compiler. But what’s that? After running

mex -setup

I got the following error:

/Applications/ line 305: gcc-4.2: command not found

What the??? No gcc? How’s that possible? I had just installed the latest shiny Xcode, everything should be a-ok! But noooo, apparently Apple believes that real developers don’t need command line tools anymore. They have removed the command line utilities from their standard Xcode install as of version 4.2. As I have version 4.3 installed, that means me. And installing it is hidden quite well. Here’s how to get them (back): in Xcode: Preferences --> Downloads With "Components" selected, you will see a list of downloadable components. Look for Command Line Tools, and click on install.

Great. That’s it, gcc is back. Good old gcc. So, we’re fine, right? Surely Matlab will work fine now, right? Wrong again. First of all, for some obscure reason, Apple has done away with the /Developer directory. Instead, it’s found in /Applications/
MacOSX.platform/Developer/. I suspect it always was in a more complicated sounding directory than /Developer, and /Developer just used to be a symlink to that. For some reason, it is no more. But hey, I’m root on this machine, so let’s just bring it back:

sudo ln -s /Applications/
MacOSX.platform/Developer/ /Developer

And that’s that. Or so I hoped. Matlab expects gcc-4.2. Xcode used to ship with that, but no more. Instead, it ships with a similar compiler (gcc-4.2 front-end to LLVM), that Matlab can use IFF you download and apply a patch from the Mathworks. They actually do a very good deal describing the Mex woes Mac users will encounter when ‘upgrading’ to Xcode 4.3.

Now, mex -setup finally works, and we can move onto building LibSVM. Unfortunately, this doesn’t work nicely either. First of all, the make.m file provided with LibSVM expects .obj files, instead of .o files, and secondly, it doesn’t support 64 bit arrays. And we want those, my machine learning training arrays are huge! In order to get LibSVM compiled on your 64-bit Matlab version on a Mac, I thus changed the commands in make.m to:

mex -largeArrayDims -O -c svm.cppmex -largeArrayDims -O -c svm_model_matlab.cmex -largeArrayDims -O svmtrain.c svm.obj svm_model_matlab.omex -largeArrayDims -O svmpredict.c svm.obj svm_model_matlab.omex -largeArrayDims -O libsvmread.cmex -largeArrayDims -O libsvmwrite.c

And voila, LibSVM finally build.