I am using Matlab 2016a on a MacOS 10.11 El Capitan (although I have reached the same problem on Sierra) trying to get mex to play nice with gfortran. I have followed the advice of this answer and have reinstalled the .xml files, but when I try to set up mex I get the following result. Not only does "which" not appear to find gfortran (while in the terminal it does), but it also can't find the "libgfortranbegin.a" folder. Mex does work with C. What should I try next?
mex -setup -v Fortran Verbose mode is on. ... Looking for compiler 'gfortran' ... ... Looking for environment variable 'DEVELOPER_DIR' ...No. ... Executing command 'which gfortran' ...No. ... Looking for file '/usr/local/bin/gfortran' ...Yes. ... Looking for folder '/usr/local/bin' ...Yes. ... Executing command 'which gfortran' ...No. ... Looking for file '/usr/local/bin/gfortran' ...Yes. ... Executing command '/usr/local/bin/gfortran -print-file-name=libgfortran.dylib' ...Yes ('/usr/local/gfortran/lib/libgfortran.dylib'). ... Looking for folder '/usr/local/gfortran/lib' ...Yes. ... Executing command 'which gfortran' ...No. ... Looking for file '/usr/local/bin/gfortran' ...Yes. ... Executing command '/usr/local/bin/gfortran -print-file-name=libgfortranbegin.a' ...Yes ('libgfortranbegin.a'). ... Looking for folder 'libgfortranbegin.a' ...No. Did not find installed compiler 'gfortran'. ... Looking for compiler 'Intel Fortran' ... ... Looking for environment variable 'IFORT_COMPILER14' ...No. ... Looking for environment variable 'IFORT_COMPILER13' ...No. ... Executing command 'which ifort' ...No. Did not find installed compiler 'Intel Fortran'. Error using mex No supported compiler or SDK was found. For options, visit http://www.mathworks.com/support/compilers/R2016a/maci64.html.
The issue appears to be that gfortran no longer includes libgfortranbegin.a and so the part of the .xml file that checks for it is no longer correct. Discussion of libgfortranbegin at https://root-forum.cern.ch/t/cant-find-libgfortranbegin-a-when-installing-root-6-06-04/21222
But after you hack gfortran.xml to remove the references to that library, the problem remains that no matter what you do, mex forces -i8 to be added to the end of FFLAGS . That is a problem because -i8 is no longer used as a flag by gfortran.
After a bunch of poking around, I was able to work around these two problems.
Step 1 is to install gfortran for OS-X. I picked mine up at http://coudert.name/software.html
Step 2 is to edit /Applications/MATLAB_R2017b.app/bin/maci64/mexopts/gfortran.xml (or as appropriate for your MATLAB version.) Replacement version is enclosed; unzip and put into that directory.
Note: I made several changes compared to the MATLAB provided version from the Question you linked to. I removed the several references to libgfortranbegin, and I added support for XCode SDK 10.12, and I coded the name of the fortran compiler as gfortrani8 in the FC assignment in the "vars" section. The changes I made to libgfortranbegin and SDK 10.12 should not give anyone any trouble, but the gfortrani8 that I introduced does not exist elsewhere.
Step 3 ... well if you installed from that source I gave, just assume the answer you need here is /usr/local/gfortran/bin and go on to Step 4.
Otherwise, go to a terminal window and in a shell (bourne shell family, not csh or zsh) execute
readlink $(which gfortran)
If it gives you back a non-empty answer then the result you need from this step is the directory of what it returns. For example if it returns /usr/local/gfortran/bin/gfortran then the answer you need is /usr/local/gfortran/bin
(If someone using Linux is trying to get some hints from this message, then readlink might not work for you: in that case
ls -l $(which gfortran)
and take the directory of what is returned.)
What you need here is the name of the directory that the gfortran executables are really installed in, which is probably the same place that gcc is really installed in. This will likely not be /usr/local/bin even though there may be a /usr/local/bin/gfortran -- that probably points off to the real directory.
Step 4: download the file gfortrani8.zip that I enclose, and unzip it. It is a shell script. Now copy that shell script into the directory found through step 3. For example it might become /usr/local/gfortran/bin/gfortrani8 . Then, at the shell, chmod +x the script.
If you received this message when you mex'd:
Error using mex gfortran: error: unrecognized command line option â-i8â; did you mean â-k8â?
then you need to be sure to install this gfortrani8 script.
If you receive this message:
Error using mex /usr/local/gfortran/bin/gfortrani8: No such file or directory
then you did successfully install my gfortran.xml but you did not install my gfortrani8 (or did not get it into the right place.)
Step 5: go into MATLAB and
and do not worry if it says it did not already exist.
Step 6: in MATLAB,
mex -setup fortran
If it tells you the compiler was found, then you should be able to proceed with compiling.
Step 7: In MATLAB you will probably need to,
setenv('DYLD_LIBRARY_PATH', [matlabroot(),'/bin/maci64:matlabroot/sys/os/maci64:', getenv('DYLD_LIBRARY_PATH')])
If your tests work, you might want to consider putting that into your startup.m file
Step 8: in at least some cases, when you compile, instead of just
you might need to
mex -client engine FileName.F
If you got the message
Error using mex Undefined symbols for architecture x86_64: "_mexfunction_", referenced from: -exported_symbol[s_list] command line option ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
when you mex'd, then this is probably the cure.
Step 9: run your code. If you did use the -client engine option then what was produced will be an external executable that you would execute with the system() or ! commands.
Step 10: curse when your program runs part way and then crashes...
Please note that gfortran is not a supported compiler on MATLAB R2016a on Mac OS. Refer the below link for more information: