Setup mex with GFORTRAN on Mac

May 8, 2017

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.

  1. 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
    
  2. Edit mex_FORTRAN_maci64.xml with sudo permission.

    1. 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"
      
    2. 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="/" />
      
    3. 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" />
      
    4. 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" />
      

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

Discussion

I'm a faculty at Fudan University. Follow me on Github and Bitbucket. Hopefully, you will find my code useful. You are also welcome to either email me or post comments below to discuss on these posts.