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

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?

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

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 :)

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

Related

Unable to compile C++ code after Xcode upgrade: <sys/cdefs.h> no such file or directory

After upgrading macOS BigSur to 11.1 and Xcode to 12.3 this morning my MacPorts 2.6.4 distribution started to have problems. I can no longer compile a simple C++ program with the GNU C++ v10.2.0_4:
g++ -O3 -std=c++11 -c libvec.cpp
In file included from /opt/local/include/gcc10/c++/cmath:45,
from libvec.cpp:1:
/opt/local/lib/gcc10/gcc/x86_64-apple-darwin20/10.2.0/include-fixed/math.h:45:10: fatal error: sys/cdefs.h: No such file or directory
45 | #include <sys/cdefs.h>
| ^~~~~~~~~~~~~
compilation terminated.
Preliminary internet search suggests that this problem may be caused by the inconsistency with the Xcode command line tools. Would you please direct me, on how to debug and fix it? Maybe I need to reinstall the MacPorts distribution after such an upgrade? Thank you!
This issue is indeed caused by the macOS update from 11.0 to 11.1 and Xcode update from 12.2 to 12.3. Apple's new policy is to increment the SDK version with each system update. The MacPorts GNU C++ compiler installed with a previous SDK version fails now due to this increment. A quick workaround is to set the SDKROOT environment variable to match the new version number:
export SDKROOT=`xcrun --show-sdk-path`
Place this command into your ~/.profile file for convenience. Detailed issue explanation and this solution may be found in the following thread on MacPorts mailing list.

Invalid glmnet Mex file in matlab

I am running glmnet package on MATLAB 2019a in macOS 10.14.5. I have also installed Xcode in my laptop.
I got the error like the following:
Invalid MEX-file '/Users/Desktop/Research/Paper Code/glmnet/glmnetMex.mexmaci64' : dlopen(/Users/Desktop/Research/Paper Code/glmnet/glmnetMex.mexmaci64,
6): Library not loaded: #loader_path/libmex.dylib
Referenced from: /Users/Desktop/Research/Paper Code/glmnet/glmnetMex.mexmaci64
I have tried the code mex -setup and got
MEX configured to use 'Xcode with Clang' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements.
In the near future you will be required to update your code to utilize the new API.
You can find more information about this at: http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
I've found some advice from mathworks specific to mex but I am not sure whether this can solve my problem. I stopped here since I am not sure if I should follow this answer to delete something.
Any ideas?
The glmnetMex MEX file included with the Glmnet download looks busticated on newer versions of macOS because it was compiled on a much older version of macOS (OS X 10.8, according to their website), and the DLL library loading mechanism has changed since then. (It should be using #rpath instead of #loader_path.) You will need to rebuild the MEX file yourself, or contact the Glmnet maintainers and ask them to provide a new build.
To rebuild the MEX file:
Set up Matlab with mex -setup to use a compiler that can compile Fortran MEX files. (I don't actually know how to do this.)
In Matlab, cd to the glmnet_matlab directory, !rm glmnetMex.mexmaci64, and then run mex glmnetMex.F
I would consider this a bug in the Glmnet distribution; I have reported it upstream to the Glmnet maintainers.
(I can reproduce this issue with a fresh download of Glmnet on my macOS 10.14.6 box running Matlab R2019a, too.)
Here's a similar problem with another Matlab library's pre-built MEX file: Library not loaded: #loader_path/libmex.dylib in matlab. Next time try Googling for various parts of your error message; searching for "Library not loaded: #loader_path/libmex.dylib" will pull up that question.
#Andrew Janke's answer worked well, except for the second step where I needed to compile using
mex FFLAGS='-fdefault-real-8 -ffixed-form -compatibleArrayDims' glmnetMex.F GLMnet.f
See also here: https://github.com/growlix/glmnet_matlab
I am running MATLAB 2019a with MacOS 10.15.3.

How to fix error: invalid value 'f95' in '-x f95'?

I'm using Fortran with CodeBlocks IDE, and when I try to build my code I get this error
error: invalid value 'f95' in '-x f95'
and after that there is no executable file. The thing is, when I write in the
terminal
gfortran nameofmyfile.f
I get the correct executable file "a.out".
Question: How can I fix this problem?
Especifications: I have mac OS X 10.8.5, installed xcode, gcc, and gfortran.
Your IDE uses the clang compiler which does not support Fortran.
See also https://answers.launchpad.net/mg5amcnlo/+question/698961 and https://developer.apple.com/forums/thread/70010.

gcc: error trying to exec 'as': execvp: No such file or directory

I was trying to install gcc and gfortran on my intel mac with mountain lion and keep getting the above error when trying to compile the fortran file. gcc doesn't seem to work either with c programs. This is the error I get with my c program:
test.c:1:19: fatal error: stdio.h: No such file or directory
I tried typing this into terminal:
export PATH=${PATH}:/usr/local/bin
but that did not work. I added gcc and gfortran to /usr/local/bin instead of /usr/bin. I downloaded the compilers through hpc. The files had the given directory structure:
/usr/local/bin: contained gcc and gfortran along with other compilers
/uer/local/include/...
/usr/local/lib/...
etc...
I pretty much just copied and pasted all the files directly over to the exact same path directories on my computer and ran the export command. That is all I have done.
You probably need either odcctools (as provided by macports) or XCode (which contains the Mac "binutils", and the Clang compiler, GCC was discontinued).

OSX 10.8, Xcode 4.4 Make and gcc gone from environment

I just upgraded to Mountain Lion OSX 10.8 and along with that I foolishly upgraded to Xcode 4.4.
However, after this upgrade "make" is gone and things like gcc -v also do not work.
This is a big thing since I am writing my phD and I rely on make to compile my LaTeX docs...
Downloading the "command line tools"
http://adcdownload.apple.com/Developer_Tools/xcode_4.4_gm_seed/cltools10_8gmseed6938077a.dmg
Is apparently not allowed for non-paying dev accounts.
What kind of foolishness is this?
They're not gone, they've just been relocated to inside Xcode's app bundle. This is actually nicer as it allows side by side installs of different XCode/SDK versions.
You can find them at: /Applications/Xcode.app/Contents/Developer/usr/bin
Also check out the xcode-select tool to allow you to choose the current active toolchain path.
I did the same thing as you this morning. To fix it, I just added the following to my ~/.bash_profile:
export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
Interestingly,
The "Downloads" interface inside Xcode 4.4 seems to point to the following location for downloading the "Command line tools":
http://adcdownload.apple.com/Developer_Tools/xcode_4.4_gm_seed/cltools10_8gmseed6938077a.dmg
Judging by the "gm" reference in that URL I think this is an error. Without a paying dev account you cannot download this...
I issued the export PATH command, and then tried to compile hello.c, but compilation fails because it does not find the file stdio.h. So, I changed the include statement to specify the full path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h, but then compilation fails because it does not find 3 other .h files.
export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
cat hello.c
#include <stdio.h>
int main()
{
printf("Hello World \n");
}
gcc hello.c -o hello
hello.c:1:19: error: stdio.h: No such file or directory
hello.c: In function ‘main’:
hello.c:4: warning: incompatible implicit declaration of built-in function ‘printf’
gcc hello.c -o hello
In file included from hello.c:2:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:64:23: error: sys/cdefs.h: No such file or directory
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:65:26: error: Availability.h: No such file or directory
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:67:20: error: _types.h: No such file or directory
Old thread, but I was just looking for this myself in Xcode 4.6:
Install Xcode
Go to Preferences->Downloads->Components and there will be an "Install" button next to "Command Line Tools"

Resources