MATLAB: Symbol error using mex in Yosemite

fortranMATLABmex

When running a compiled mex-fortran file, I get a "Symbol not found" error. I think it's a linking error, but i don't know how to fix it. Any suggestions? Here is the error:
Invalid MEX-file '/Users/scott/Documents/Code/Mfiles-other/mex/band/band.mexmaci64':
dlopen(/Users/scott/Documents/Code/Mfiles-other/mex/band/band.mexmaci64, 6): Symbol not
found: __gfortran_transfer_integer_write
Referenced from: /Users/scott/Documents/Code/Mfiles-other/mex/band/band.mexmaci64
Expected in: /Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
in /Users/scott/Documents/Code/Mfiles-other/mex/band/band.mexmaci64
I'm compiling using the "mex" command within MATLAB 2014b, using fortran 4.9.1, installed as part of homebrew gcc 4.9.1, on Max OS Yosemite. The fortran file seems to compile fine, and I've copied the output of "mex -v band.F" below.
>> mex -v band.F
Verbose mode is on.
Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require the use of
-largeArrayDims and remove the -compatibleArrayDims option.
For more information:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
No MEX options file identified; looking for an implicit selection.
... 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/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/libgfortran.dylib').
... Looking for folder '/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1' ...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 ('/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/libgfortranbegin.a').
... Looking for folder '/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1' ...Yes.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk' ...Yes.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk' ...No.
... Looking for folder '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk' ...Yes.
... Executing command 'echo /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk | rev | cut -c1-10 | rev | egrep -oh '[0-9]+\.[0-9]+'' ...Yes ('10.9').
Found installed compiler 'gfortran'.
Options file details
-------------------------------------------------------------------
Compiler location: /usr/local/bin
Options file: /Applications/MATLAB_R2014b.app/bin/maci64/mexopts/gfortran.xml
CMDLINE1 : /usr/local/bin/gfortran -c -DMX_COMPAT_32 -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include" -fexceptions -m64 -fbackslash -O /Users/scott/Documents/Code/Mfiles-other/mex/band/band.F -o /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
CMDLINE2 : xcrun -sdk macosx10.9 clang -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/fexport.map" -O /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o -L"/Applications/MATLAB_R2014b.app/bin/maci64" -lmx -lmex -lmat -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortran -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortranbegin -o band.mexmaci64
CMDLINE3 : rm -f /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
FC : /usr/local/bin/gfortran
FDEFINES : -DMX_COMPAT_32
FFLAGS : -fexceptions -m64 -fbackslash
INCLUDE : -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include"
FOPTIMFLAGS : -O
FDEBUGFLAGS : -g
LDF : xcrun -sdk macosx10.9 clang
LDFLAGS : -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/fexport.map"
LDBUNDLE : -bundle
LINKEXPORT : -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/fexport.map"
LINKLIBS : -L"/Applications/MATLAB_R2014b.app/bin/maci64" -lmx -lmex -lmat -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortran -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortranbegin
LDOPTIMFLAGS : -O
LDDEBUGFLAGS : -g
OBJEXT : .o
LDEXT : .mexmaci64
DEVELOPER_DIR_CHECK :
GFORTRAN_INSTALLDIR : /usr/local/bin
GFORTRAN_LIBDIR : /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
GFORTRANBEGIN_LIBDIR : /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
ISYSROOT : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
SDKVER : 10.9
MATLABROOT : /Applications/MATLAB_R2014b.app
ARCH : maci64
SRC : /Users/scott/Documents/Code/Mfiles-other/mex/band/band.F
OBJ : /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
OBJS : /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
SRCROOT : /Users/scott/Documents/Code/Mfiles-other/mex/band/band
DEF : /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.def
EXP : band.exp
LIB : band.lib
EXE : band.mexmaci64
ILK : band.ilk
MANIFEST : band.mexmaci64.manifest
TEMPNAME : band
EXEDIR :
EXENAME : band
OPTIM : -O
LINKOPTIM : -O
-------------------------------------------------------------------
Building with 'gfortran'.
/usr/local/bin/gfortran -c -DMX_COMPAT_32 -I"/Applications/MATLAB_R2014b.app/extern/include" -I"/Applications/MATLAB_R2014b.app/simulink/include" -fexceptions -m64 -fbackslash -O /Users/scott/Documents/Code/Mfiles-other/mex/band/band.F -o /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
xcrun -sdk macosx10.9 clang -Wl,-twolevel_namespace -undefined error -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -bundle -Wl,-exported_symbols_list,"/Applications/MATLAB_R2014b.app/extern/lib/maci64/fexport.map" -O /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o -L"/Applications/MATLAB_R2014b.app/bin/maci64" -lmx -lmex -lmat -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortran -L"/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1" -lgfortranbegin -o band.mexmaci64
rm -f /var/folders/_2/cdmj3bs94gx6r569mvsqqblm0000gn/T//mex_11838655149306_12289/band.o
MEX completed successfully.

Best Answer

Scott - I observed a similar error when I tried to run a Fortran-based MEX function that included some code to write data to the console (via mexPrintf). To get around this problem, I had to move my copy of libgfortran.3.dylib, substituting a different version using a symbolic link.
In my /Applications/MATLAB_R2014a.app/sys/os/maci64 folder, I did the following
cd /Applications/MATLAB_R2014a.app/sys/os/maci64
mv libgfortran.3.dylib libgfortran.3.dylib.old
ln -s /usr/local/gfortran/lib/libgfortran.3.dylib libgfortran.3.dylib
I then quit MATLAB, restarted it, and was able to run the MEX function with no errors.
Related Question