5

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?

Tim
  • 1,466
  • 16
  • 24
Bryan P
  • 5,900
  • 5
  • 34
  • 49

3 Answers3

11

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

Bryan P
  • 5,900
  • 5
  • 34
  • 49
  • Thanks for solving your problem **and** posting your answer. It worked for me! – Tim Dec 21 '12 at 10:54
  • @Tim: regarding your edit in [leif's answer](http://stackoverflow.com/questions/893657/how-do-i-calculate-r-squared-using-python-and-numpy/895063#895063). If you think that the answer can be improved, edit it, but add your code below so it's obvious which answer/code was accepted and what is the suggested improvement. Or, alternatively, you can add an answer of your own. – ypercubeᵀᴹ Dec 21 '12 at 15:44
  • @ypercube: I added an error message I got in the same situation, so others can find it. I didn't have Matlab code, I was trying to compile a module (SeDuMi) on OS X 10.7.5 and got similar errors. I'm not sure if adding that clarifies things, which is why I added it to the initial question. – Tim Dec 21 '12 at 15:49
  • @Tim: Anyway, that edit was rejected (I think). So, make another try by editing or commenting. I'll delete these comments, too, Bryan sorry, for the unrelated discussion. – ypercubeᵀᴹ Dec 21 '12 at 16:10
5

I am running Mac Mountain Lion and Matlab R2012b, and the following solution worked for me: Matlab 2012a Mex to work with Xcode 4.5 on Mountain Lion

To be precise, I was getting the following errors:

error: stdio.h: No such file or directory

error: stdlib.h: No such file or directory

error: math.h: No such file or directory

So what I did to solve this issue:

I installed Xcode with Command Line Tools, and then edited the file "/Applications/MATLAB_R2012b.app/bin/mexopts.sh" by:

changing CC from gcc-4.2 to llvm-gcc-4.2

changing CXX from gcc-4.2 to llvm-g++-4.2

setting SDKROOT to '/'

Then I ran "mex -setup" in MATLAB, chose number 1, and chose y to overwrite the old /Users/insertyournamehere/.matlab/R2012b/mexopts.sh

That solved it :)

Community
  • 1
  • 1
fgar
  • 357
  • 1
  • 3
  • 9
  • 1
    your answer is not identical since the questioner who has posted the answer has not yet selected the answer. So can you please write it down again? – sadaf2605 Jan 14 '13 at 17:08
  • By choosing `1` you overwrite your setting you made before. So actually it is enough to rum `mex -setup`. – Michael Dorner Feb 12 '15 at 10:09
0

You will find valuable information here: http://www.mathworks.fr/support/solutions/en/data/1-FR6LXJ/

lizzie
  • 1,506
  • 1
  • 18
  • 31