我需要在MATLAB中调用一些Fortran代码。我做了一些研究并阅读了mex
命令以及如何使用它。可悲的是,我已经无法让Fortran编译器工作了。
首先,这是我的设置:
根据MATLAB文档,我可以使用mex -setup FORTRAN
准备mex
来构建Fortran的mex文件。但是,以详细模式运行命令会产生以下输出:mex -setup -v FORTRAN
Verbose mode is on.
... Looking for compiler 'Intel Fortran Composer XE' ...
... Looking for environment variable 'IFORT_COMPILER16' ...No.
... Looking for environment variable 'IFORT_COMPILER15' ...No.
... 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 Composer XE'.
Error using mex
No supported compiler or SDK was found. For options, visit
http://www.mathworks.com/support/compilers/R2016b/maci64.html.
在link之后,可以看到MATLAB在Linux上支持GNU gfortran 4.9.x
。然而,在Mac上,只有英特尔的商业编译器被列为支持。这也是mex
似乎也在寻找的东西。
由于Mac也可以使用gfortran
来编译Fortran代码,我认为有可能让它与MATLAB一起工作。我也在谷歌搜索了很多,并在MathWorks论坛上发现了this one之类的问题,这表明MATLAB应该能够使用gfortran
,即使在Mac上也是如此。
我觉得这很奇怪,我的MATLAB甚至都没有找gfortran
编译器。所有这一切都是寻找一个英特尔编译器,找不到一个,然后抛出上述错误信息。
关于我的gfortran
安装。它绝对是4.9.2(在Linux下被列为支持),which gfortran
返回/usr/local/bin
,我可以通过终端成功编译程序。
顺便说一句,mex -setup ANY
成功列出了C和C ++的编译器,但没有Fortran。
MEX configured to use 'Xcode with Clang' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
MEX configured to use 'Xcode Clang++' for C++ language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different C compiler, select one from the following:
Xcode with Clang mex -setup:'/Users/Lennart/Library/Application Support/MathWorks/MATLAB/R2016b/mex_C_maci64.xml' C
Xcode Clang++ mex -setup:'/Users/Lennart/Library/Application Support/MathWorks/MATLAB/R2016b/mex_C++_maci64.xml'
我还查看了最后一个输出末尾提到的.xml
个文件。没有任何与Fortran有关的文件,我自己也无法成功编写。我甚至不确定这是不是问题......
所以简单地说我的问题是:如何让MATLAB实际查找,然后当然也找到我的gfortran
编译器来使用它来编译mex
个文件?
感谢您的帮助!
答案 0 :(得分:4)
感谢hsuyaa和提供的链接,我能够解决我的问题。因为它需要我自己进行更多的实验,所以我想发布我是如何能够让gfortran工作的。
查看和接受的答案。尽管MathWorks团队明确指出这些指令是专门为xCode 7.0和MATLAB R2015b编写的,但我还是可以使用xCode 8.1,MATLAB R2016b和macOS Sierra 10.12。
MATLAB似乎将编译器配置细节存储在.xml文件中,如前所述。您可以通过输入
在MATLAB中找到该目录cd( fullfile( matlabroot, 'bin', 'maci64', 'mexopts' ) );
之前我确实执行了全新的MATLAB安装,但是在那个位置,只有三个文件位于其中,一个用于Clang,一个用于Clang ++,一个用于英特尔的Fortran。 gfortran只是失踪了。
MathWorks的解决方案作者将所有这些.xml文件附加到他的帖子中。我下载了文件并将gfortran.xml复制到上面提到的文件夹中。这获得了部分成功,因为它使MATLAB在运行mex设置时实际上寻找gfortran。
但是,由于文件不是最新的,我不得不添加几行。我并不完全了解配置文件的工作原理,但我注意到有些行提到了较旧的macOS版本。下载上面链接后面的文件gfortran.xml,然后添加以下内容:
无论你在哪里看到
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk" />
或
<cmdReturns name="find $$ -name MacOSX10.11.sdk" />
还要添加
<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk" />
或
<cmdReturns name="find $$ -name MacOSX10.12.sdk" />
macOS 10.12是Sierra。保存文件并再次成功运行mex -setup FORTRAN
识别出gfortran并将其设置为mex的fortran编译器。
答案 1 :(得分:-1)
您的matlab版本无法检测支持的编译器。可能是这个Link可以提供帮助。