MATLAB MEX can't find standard library with XCode 4.3 (Mac)

Bryan P picture Bryan P · Mar 23, 2012 · Viewed 8.6k times · Source

I am getting started with using MEX files for MATLAB (R2012a) compiled from C code (using XCode 4.3) on my Mac (running OSX 10.7.3, Lion). I have already installed the MATLAB provided XCode patch which configures MATLAB to use the new llvm-gcc compiler under XCode 4.2+, but I am still getting the following error attempting to compile the example file using mex timestwo.c:

/Applications/MATLAB_R2012a.app/extern/include/matrix.h:852:20: error: stdlib.h: No such file or directory
In file included from timestwo.c:1:
/Applications/MATLAB_R2012a.app/extern/include/mex.h:161:19: error: stdio.h: No such file or directory

The compilation then breaks

mex: compile of ' "timestwo.c"' failed.

Error using mex (line 206)
Unable to complete successfully.

Clearly, the compiler can't find the standard library header directory, but what do I need to change to successfully compile?

Answer

Bryan P picture Bryan P · Mar 24, 2012

Ah, found it!

The MATLAB provided template uses /Developer/... for the SDK location, but this changed under XCode4.3 to live under the XCode.app. To fix things, I had to edit my ~/.matlab/R2012a/mexopts.sh file to set the SDKROOT directory to the new location.

To do this, search for SDKROOT in the mexopts.sh file and change it to read:

SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/'

Note, I chose the 10.7 sdk b/c I am on Lion. Use 10.6 if you are on Snow Leopard