在带有 Matlab 2017b 的 win10 上使用 gfortran

问题描述

我想在 Matlab 2017b、Win10 中编译 fortran 代码以获取 mex 文件。有谁知道如何绕过matlab在选择Intel编译器时的限制并强制它改用gfortran?

我有一个简单的 Fortran 代码示例,“timestwo.f”。

#include "fintrf.h"
C======================================================================
#if 0
C     
C     timestwo.F
C     .F file needs to be preprocessed to generate .for equivalent
C     
#endif
C     
C     timestwo.f
C
C     Computational function that takes a scalar and doubles it.
      
C     This is a MEX-file for MATLAB.
C     Copyright 1984-2011 The MathWorks,Inc.
C     
C======================================================================
C     Gateway routine
      subroutine mexFunction(nlhs,plhs,nrhs,prhs)

C     Declarations
      implicit none

C     mexFunction arguments:
      mwPointer plhs(*),prhs(*)
      integer nlhs,nrhs

C     Function declarations:
      mwPointer mxGetPr
      mwPointer mxCreateDoubleMatrix
      integer mxIsNumeric
      mwPointer mxGetM,mxGetN

C     Pointers to input/output mxArrays:
      mwPointer x_ptr,y_ptr

C     Array information:
      mwPointer mrows,ncols
      mwSize size

C     Arguments for computational routine:
      real*8  x_input,y_output

C-----------------------------------------------------------------------
C     Check for proper number of arguments. 
      if(nrhs .ne. 1) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:nInput',+                           'One input required.')
      elseif(nlhs .gt. 1) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:nOutput',+                           'Too many output arguments.')
      endif

C     Validate inputs
C     Check that the input is a number.
      if(mxIsNumeric(prhs(1)) .eq. 0) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric',+                           'Input must be a number.')
      endif

C     Get the size of the input array.
      mrows = mxGetM(prhs(1))
      ncols = mxGetN(prhs(1))
      size = mrows*ncols

C     Create Fortran array from the input argument.
      x_ptr = mxGetPr(prhs(1))
      call mxCopyPtrToReal8(x_ptr,x_input,size)

C     Create matrix for the return argument.
      plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)
      y_ptr = mxGetPr(plhs(1))

C     Call the computational subroutine.
      call timestwo(y_output,x_input)

C     Load the data into y_ptr,which is the output to MATLAB.
      call mxCopyReal8ToPtr(y_output,y_ptr,size)     

      return
      end

C-----------------------------------------------------------------------
C     Computational routine

      subroutine timestwo(y_output,x_input)
      real*8 x_input,y_output

      y_output = 2.0 * x_input
      return
      end

官方在 Windows 中发布的此 Matlab 版本仅支持英特尔的商业 Fortran 编译器 ifor。有什么解决方法吗?我最初将 MinGW 安装为 matlab 的官方插件,但我注意到这个特定版本不包含 gfortran。

因此我手动安装了相同版本(但使用 gfortran)并相应地更改了环境变量。

Mex 生成的 C 代码运行顺利,但是当我尝试选择 g-fortran 时我得到了这个

>> mex -setup
MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
     variables with more than 2^32-1 elements. 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 language,select one from the following:
 mex -setup C++ 
 mex -setup FORTRAN
Error using mex
No supported compiler or SDK was found. For options,visit https://www.mathworks.com/support/compilers.

因此,当我尝试编译该函数时,它只会查找英特尔的编译器。

>> mex -v timestwo.F
Verbose mode is on.
Warning: MATLAB FORTRAN MEX Files are now defaulting to -largeArrayDims and 8 byte integers.
     If you are building a FORTRAN S-Function,please recompile using the -compatibleArrayDims flag.
     You can find more about adapting code to use 64-bit array dimensions at:
     http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html. 
No MEX options file identified; looking for an implicit selection.
... Looking for compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft SDK 7.1' ...
... Looking for environment variable 'IFORT_COMPILER14' ...No.
... Looking for environment variable 'IFORT_COMPILER13' ...No.
Did not find installed compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft SDK 7.1'.
... Looking for compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft Visual Studio 2013' ...
... Looking for environment variable 'IFORT_COMPILER14' ...No.
... Looking for environment variable 'IFORT_COMPILER13' ...No.
Did not find installed compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft Visual Studio 2013'.
... Looking for compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft SDK 7.1' ...
... Looking for environment variable 'IFORT_COMPILER15' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft SDK 7.1'.
... Looking for compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft Visual Studio 2013' ...
... Looking for environment variable 'IFORT_COMPILER15' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft Visual Studio 2013'.
... Looking for compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft Visual Studio 2015' ...
... Looking for environment variable 'IFORT_COMPILER15' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2015 for Fortran with Microsoft Visual Studio 2015'.
... Looking for compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft SDK 7.1' ...
... Looking for environment variable 'IFORT_COMPILER16' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft SDK 7.1'.
... Looking for compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft Visual Studio 2013' ...
... Looking for environment variable 'IFORT_COMPILER16' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft Visual Studio 2013'.
... Looking for compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft Visual Studio 2015' ...
... Looking for environment variable 'IFORT_COMPILER16' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2016 for Fortran with Microsoft Visual Studio 2015'.
... Looking for compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft SDK 7.1' ...
... Looking for environment variable 'IFORT_COMPILER17' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft SDK 7.1'.
... Looking for compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2013' ...
... Looking for environment variable 'IFORT_COMPILER17' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2013'.
... Looking for compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2015' ...
... Looking for environment variable 'IFORT_COMPILER17' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2015'.
... Looking for compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2017' ...
... Looking for environment variable 'IFORT_COMPILER17' ...No.
Did not find installed compiler 'Intel Parallel Studio XE 2017 for Fortran with Microsoft Visual Studio 2017'.
Error using mex
No supported compiler or SDK was found. For options,visit https://www.mathworks.com/support/compilers.

有人设法改用 gfortran 吗?

提前致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...