How to set up mex with gfortran on Mac?

조회 수: 29 (최근 30일)
Thomas Gebhardt
Thomas Gebhardt 2017년 5월 2일
댓글: dpb 2021년 12월 18일
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.

답변 (3개)

Walter Roberson
Walter Roberson 2017년 10월 14일
편집: Walter Roberson 2018년 2월 23일
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
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...
  댓글 수: 8
dpb
dpb 2021년 12월 5일
Any reason you couldn't just build the mex file outside the mex utility, Walter? If you can find all the pieces of the make file to know all the libraries and all...this having turned mex into a builtin and not being able to mung it is a real kick in the teeth. TMW ought to be ashamed of themselves in this area.
I found this thread while thinking about what it might take to manage to get gfortran to work on Windows again where it is "unsupported". As another said, just because TMW doesn't want to answer calls doesn't mean they should actively make it essentially impossible for the user...and there seems virtually no rational reason not to support gfortran when they do support gcc and all gfortran is is the front end language parser for gcc.
It's simply unconscionable on their part in my view...
Vahagn Davtyan
Vahagn Davtyan 2021년 12월 18일
Hi,
I try to follow Your steps, but when I run:
mex -setup fortran
I get an error:
Error using mex
No supported compiler was found. For options, visit https://www.mathworks.com/support/compilers.
I am using Matlab R2020b and macOS 12.0.1 (Monterey)

댓글을 달려면 로그인하십시오.


Sanjana Ramakrishnan
Sanjana Ramakrishnan 2017년 5월 4일
편집: Walter Roberson 2021년 8월 28일
Please note that gfortran is not a supported compiler on MATLAB R2016a on Mac OS. Refer the below link for more information:
  댓글 수: 3
dpb
dpb 2021년 8월 27일
Amen to that!
dpb
dpb 2021년 12월 6일
I'll also note that just looked to see and the ML download in Apps for the MATLAB Compiler downloads and installs gfortran.
I just compiled an ran successfully the minimal "Hello, World!" program so it's hard to see what's keeping TMW from not allowing mex Fortran.
I've not tried to see if can build mex from command line; I may give that a go if get a little extra time more to dig into the make files to find the TMW libraries to link.
The problem I'm afraid am going to run into again is the poison pill of the embedded version stuff...

댓글을 달려면 로그인하십시오.


Daming Lou
Daming Lou 2018년 4월 18일

Hi

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" ( https://nl.mathworks.com/help/matlab/matlab_external/symbol-mexfunction-unresolved-or-not-defined.html ).

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

Any suggestion will be welcome! Thanks!.

  댓글 수: 1
dpb
dpb 2021년 12월 18일
That indicates the linker couldn't find the entry point in the mxlib library.
If this is the only unsatisfied external, is this the only call in the mex file to a mx function? That would seem highly unlikely, but if the problem is that the mxlib object file isn't in the search path and it were the only function trying to link to, then fixing that might resolve the issue....
But, if as is more likely, the problem is that ony this one mx routine didn't link but the others did, then the compiler generated the object name inconsistently with those names exposed in the mx object library. That kind of an issue is going to be much harder to resolve if were to be so.
The fortran include file creates a bunch of macros that create naming conventions for all the functions that are based on the detected compiler and MATLAB version and OS -- there are different naming conventions used depending on those including capitalization and whether or not the underscores are/are not used, etc., etc., that all have to agree with the calling convention used when compiling the libraries.
I don't have Mac and the experiments I outlined above to try to use GFortran on Windows got to be more involved than I had time to continue to pursue so no specifics to offer other than that's what the errror is telling you -- it couldn't find the subject function in the mx library.
Can you successfully compile/run one of the simple Fortran examples like timestwo to see if a base trivial mex function will work?
You'll get more info to help others who may have Mac if you'll rerun the mex command with the "-v" flag and post the whole response...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by