【问题标题】:MATLAB mex not looking for compiler gfortran on macOSMATLAB mex 不在 macOS 上寻找编译器 gfortran
【发布时间】:2017-04-23 02:42:02
【问题描述】:

我需要从 MATLAB 中调用一些 Fortran 代码。我做了一些研究并阅读了mex 命令以及如何使用它。遗憾的是,我已经无法让 Fortran 编译器工作了。

首先,这是我的设置:

  • macOS Sierra 10.12.1(最新版本)
  • MATLAB R2016b(最新版本)
  • gfortran 4.9.2(通过 GNU 官方站点的 .dmg 安装)

根据 MA​​TLAB 文档,我可以使用 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


    【解决方案1】:

    感谢 hsuyaa 和提供的链接,我能够解决我的问题。由于它需要我自己进行更多的实验,我想发布一下我是如何让 gfortran 工作的。

    查看this link 和接受的答案。尽管 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++,一个用于 Intel 的 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 编译器。

    【讨论】:

      【解决方案2】:

      您的 matlab 版本无法检测到支持的编译器。可能是这个Link 可以提供帮助。

      【讨论】:

      • 您的链接可能包含正确的解决方案,但您也应该以某种方式在您的答案中描述它。很多人真的很讨厌这样的答案并投反对票。例如,请参阅meta.stackoverflow.com/questions/265552/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多