MATLAB mex 不在 macOS 上寻找编译器 gfortran
MATLAB mex not looking for compiler gfortran on macOS
我需要从 MATLAB 中调用一些 Fortran 代码。我做了一些研究并阅读了 mex
命令及其使用方法。遗憾的是,我已经无法让 Fortran 编译器正常工作了。
首先,这是我的设置:
- macOS Sierra 10.12.1(最新版本)
- MATLAB R2016b(最新版)[=56=]
- 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 一起工作。我也用谷歌搜索了很多,在 MathWorks 论坛上发现了像 this one 这样的问题,这表明 MATLAB 应该能够使用 gfortran
,即使在 Mac.
上
这就是我认为奇怪的地方,我的 MATLAB 甚至没有在寻找 gfortran
编译器。它所做的只是寻找英特尔编译器,找不到然后抛出上述错误消息。
关于我的 gfortran
安装。它肯定是 4.9.2(在 Linux 下被列为支持),which gfortran
returns /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
文件?
感谢您的帮助!
您的 matlab 版本无法检测到支持的编译器。也许这个 Link 可以提供帮助。
感谢 hsuyaa 和提供的 link 我能够解决我的问题。由于它需要我自己进行更多试验,因此我想 post 我究竟是如何让 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 文件附加到他的 post。我下载了文件并将 gfortran.xml 复制到上述文件夹中。这取得了部分成功,因为它使 MATLAB 在 运行 mex 设置时实际寻找 gfortran。
但是,由于文件不是最新的,我不得不添加几行。我不完全了解配置文件的工作原理,但我注意到有些行引用了较旧的 macOS 版本。下载上面link后面的文件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 编译器。
我需要从 MATLAB 中调用一些 Fortran 代码。我做了一些研究并阅读了 mex
命令及其使用方法。遗憾的是,我已经无法让 Fortran 编译器正常工作了。
首先,这是我的设置:
- macOS Sierra 10.12.1(最新版本)
- MATLAB R2016b(最新版)[=56=]
- 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 一起工作。我也用谷歌搜索了很多,在 MathWorks 论坛上发现了像 this one 这样的问题,这表明 MATLAB 应该能够使用 gfortran
,即使在 Mac.
这就是我认为奇怪的地方,我的 MATLAB 甚至没有在寻找 gfortran
编译器。它所做的只是寻找英特尔编译器,找不到然后抛出上述错误消息。
关于我的 gfortran
安装。它肯定是 4.9.2(在 Linux 下被列为支持),which gfortran
returns /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
文件?
感谢您的帮助!
您的 matlab 版本无法检测到支持的编译器。也许这个 Link 可以提供帮助。
感谢 hsuyaa 和提供的 link 我能够解决我的问题。由于它需要我自己进行更多试验,因此我想 post 我究竟是如何让 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 文件附加到他的 post。我下载了文件并将 gfortran.xml 复制到上述文件夹中。这取得了部分成功,因为它使 MATLAB 在 运行 mex 设置时实际寻找 gfortran。
但是,由于文件不是最新的,我不得不添加几行。我不完全了解配置文件的工作原理,但我注意到有些行引用了较旧的 macOS 版本。下载上面link后面的文件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 编译器。