AudioPlastic

Compile Mex Files With XCode 5 on OSX Mavericks Using Older Versions of Matlab

| Comments

So, you’ve upgraded OSX to the latest version and also upgraded XCode to 5.X. You’ll find that the Matlab mex command now fails to build your Matlab executables files as compiler related stuff has moved and version numbers have changed. Thankfully, the fix is relatively simple. I have tried this using 2011b (which is quite out of date now), but I’m guessing that this fix will work for most Matlab versions.

Just locate mexopts.sh, which can be found in ~/.your_old_matlab_version/mexopts.sh, and open it with your favorite editor. Search for maci64) and then comment out everything related to the C compiler in this shell script block. This is probably everything from the line, CC=... to the line, CXXDEBUGFLAGS=... Now just prefix the commented block with this substitute.

mexopts.sh additions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CC='llvm-gcc'
SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk'
MACOSX_DEPLOYMENT_TARGET='10.9'
ARCHS='x86_64'
CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CFLAGS="$CFLAGS -fexceptions"
CLIBS="$MLIBS"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'

CLIBS="$CLIBS -lstdc++"
CXX='llvm-g++'
CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CXXLIBS="$MLIBS -lstdc++"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'

You can also use apple frameworks in your mex files, but there is no flag available for the mex tool that lets you do this from the command line. If you want to use useful stuff like the Accelerate Framework, a little more mexopts.sh tinkering needs to be done. Edit the following flags so they look like the ones shown …

mexopts.sh framework additions
1
2
CLIBS="$CLIBS -lstdc++ -framework Accelerate"
CXXLIBS="$MLIBS -lstdc++ -framework Accelerate"

Comments