Setup mex with GFORTRAN on Mac
I was struggling with mex setup on Mac for a very long time since there was
error with mex -setup C
for Matlab on new MacOS. It turned out that the
Matlab search path did not include the newest Xcode SDK “MacOSX10.12.sdk” in
its search path. Once it was added, the problem with mex -setup C
was
resolved.
However, today, I encountered another mex issue in mex -setup FORTRAN
.
When I ran mex -setup -v FORTRAN
, the following error message appeared,
>> mex -setup -v FORTRAN
Verbose mode is on.
... Looking for compiler 'gfortran' ...
... Looking for environment variable 'DEVELOPER_DIR' ...No.
... Executing command 'which gfortran' ...Yes ('/usr/local/bin/gfortran').
... Looking for folder '/usr/local/bin' ...Yes.
... Executing command 'which gfortran' ...Yes ('/usr/local/bin/gfortran').
... Executing command '/usr/local/bin/gfortran -print-file-name=libgfortran.dylib' ...Yes ('/usr/local/Cellar/gcc/6.3.0_1/lib/gcc/6/libgfortran.dylib').
... Looking for folder '/usr/local/Cellar/gcc/6.3.0_1/lib/gcc/6' ...Yes.
... Executing command 'which gfortran' ...Yes ('/usr/local/bin/gfortran').
... 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' ...Yes ('/usr/local/bin/ifort').
... Looking for folder '/usr/local/bin' ...Yes.
... Looking for environment variable 'IFORT_COMPILER14' ...No.
... Looking for environment variable 'IFORT_COMPILER13' ...No.
... Executing command 'which ifort' ...Yes ('/usr/local/bin/ifort').
... Looking for folder '/usr/compiler/lib' ...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.
To be more specific, my Fortran version is GFORTRAN (GCC 6.3.0_1) and my
MATLAB version is R2016a. The reason causing the error above is due to the
change of the Fortran library in the new GNU version. In recent versions,
GNU removed the library libgfortranbegin.a
and moved these piece of code
into libgfortran.a
. Therefore, if we need to link the mex with new
GFORTRAN version, the searching in MATLAB must be modified.
Here, we list a few steps in the modification.
-
Copy a xml file to local start up folder.
cp /Applications/MATLAB_R2016a.app/bin/maci64/mexopts/gfortran.xml ~/.matlab/R2016a/mex_FORTRAN_maci64.xml
-
Edit
mex_FORTRAN_maci64.xml
withsudo
permission.- Find the line with
LINKLIBS="-L"$MATLABROOT/bin/maci64" -lmx -lmex -lmat -L"$GFORTRAN_LIBDIR" -lgfortran -L"$GFORTRANBEGIN_LIBDIR" -lgfortranbegin"
and change it to
LINKLIBS="-L"$MATLABROOT/bin/maci64" -lmx -lmex -lmat -L"$GFORTRAN_LIBDIR" -lgfortran -lgcc_s.1"
- Find the lines as
<cmdReturns name="$$/gfortran -print-file-name=libgfortranbegin.a " /> <dirExists name="$$" />
and change it to
<cmdReturns name="$$/gfortran -print-file-name=libgfortranbegin.a " /> <dirExists name="/" />
- Find all lines with
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk" />
and attach the following line below in every place
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk" />
- Find all lines with
<cmdReturns name="find $$ -name MacOSX10.11.sdk" />
and attach the following line below in every place
<cmdReturns name="find $$ -name MacOSX10.12.sdk" />
- Find the line with
If there is minor difference between your system and mine, please update the corresponding path. If the difference is major, and you find it impossible to update accordingly, please comment below or send me an Email.
Yingzhou Li