MATLAB Answers


How to set up mex with gfortran on Mac?

Asked by Thomas Gebhardt on 2 May 2017
Latest activity Commented on by Emily Jollans on 16 Nov 2018
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


Sign in to comment.

3 Answers

Answer by Walter Roberson
on 14 Oct 2017
Edited by Walter Roberson
on 23 Feb 2018

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
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
Step 2 is to edit /Applications/ (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 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
delete(fullfile(prefdir, 'mex_FORTRAN_maci64.xml'))
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
mex FileName.F
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...


Good point, Tommaso Pino.
Also, instead of checking a bunch of SKDs and having to update the list, would it be easier to replace the search in ISYSROOT and SDKVER with the following?
"<cmdReturns name="xcrun -sdk macosx --show-sdk-path"/>"
This finds whatever SDK is installed (in my case it's 10.13). I tested this out, and mex does seem to find the correct SDK. However, I have other issues mex'ing and not sure it is related to this.
Thanks for the instruction! I managed to get gfortran working in R2018a on macos 10.14 (mojave) with some additional steps.
In the gfortran.xlm file I added my macOS version to the four places where versions are specified.
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk" />
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk" />
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk" />
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk" />
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" />
<cmdReturns name="find $$ -name MacOSX10.9.sdk" />
<cmdReturns name="find $$ -name MacOSX10.10.sdk" />
<cmdReturns name="find $$ -name MacOSX10.11.sdk" />
<cmdReturns name="find $$ -name MacOSX10.12.sdk" />
<cmdReturns name="find $$ -name MacOSX10.14.sdk" />
I also had to create the directory /Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
and copied to it the MacOSX.sdk directory from /Applications/ using the bash shell.
cp -r /Applications/ /Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
I then copied the fortani8 file to my gfortran directory which (when installed using homebrew) was /usr/local/Cellar/gcc/8.2.0/bin/ for me.
cp ../Downloads/gfortrani8 /usr/local/Cellar/gcc/8.2.0/bin/
Then run the following in MATLAB and it should work:
mex -setup -v FORTRAN

Sign in to comment.

Answer by Sanjana Ramakrishnan on 4 May 2017

Please note that gfortran is not a supported compiler on MATLAB R2016a on Mac OS. Refer the below link for more information:


Sign in to comment.

Answer by Daming Lou on 18 Apr 2018


Thanks Walter Roberson! I manage to install the C & Fortran compiler in mac (OS10.13) with Matlab 2016b.

mex -setup fortran 

works. But when I run a code from a project gives me a issue which is listed on your post

Undefined symbols for architecture x86_64:
  "_mxsetfield_", referenced from:
      _mexfunction_ in xxx.o

I have figured it out that the mxsetfield is called in the xxx.o like

        fptr = fortran_to_matlab(fact)
        call mxSetField( plhs(1), ind_i, f2, fptr )
        fptr = fortran_to_matlab(ifact)
        call mxSetField( plhs(1), ind_i, f4, fptr )
        fptr = fortran_to_matlab(cntl)
        call mxSetField( plhs(1), ind_i, f5, fptr )
        fptr = fortran_to_matlab(icntl)
        call mxSetField( plhs(1), ind_i, f6, fptr )
        fptr = fortran_to_matlab(info)
        call mxSetField( plhs(1), ind_i, f7, fptr )
        fptr = fortran_to_matlab(rinfo)
        call mxSetField( plhs(1), ind_i, f8, fptr )

I also checked the internet like crazy, it seems related to "Symbol mexFunction Unresolved or Not Defined" ( ).

I am very bad at Fortran and have no clue how I can fix.

Any suggestion will be welcome! Thanks!.


Sign in to comment.