【发布时间】:2017-04-23 02:42:02
【问题描述】:
我需要从 MATLAB 中调用一些 Fortran 代码。我做了一些研究并阅读了mex 命令以及如何使用它。遗憾的是,我已经无法让 Fortran 编译器工作了。
首先,这是我的设置:
- macOS Sierra 10.12.1(最新版本)
- MATLAB R2016b(最新版本)
- gfortran 4.9.2(通过 GNU 官方站点的 .dmg 安装)
根据 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 上,只有 Intel 的商业编译器被列为支持。这就是mex 似乎也在寻找的东西。
由于 Mac 也可以使用 gfortran 编译 Fortran 代码,我认为它可以与 MATLAB 一起使用。我也在 Google 上搜索了很多,并在 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 文件?
感谢您的帮助!
【问题讨论】:
标签: macos matlab fortran gfortran mex