使用Gnumex和MinGW的MEX fortran文件(g77)

时间:2015-03-02 19:31:01

标签: matlab fortran mex fortran77

我最近遇到了一个问题。它曾经是我记得很好的工作,但几周后我再次尝试MEX在MATLAB中的一个fortran 77文件,但它给了我一个奇怪的错误。 好的,我使用名为mexopts_f77.bat的Gnumex创建了MEX选项文件。 这是我在MATLAB中运行的命令:

mex -v -f C:\Omid\Other\Gnumex\mexopts_f77.bat -c sodex.f decsol.f

这就是我得到的错误:

-> Options file specified on command line 
---------------------------------------------------------------- 
->    Options file           = C:\Omid\Other\Gnumex\mexopts_f77.bat 
      MATLAB                 = C:\PROGRA~2\MATLAB\R2011b 
->    COMPILER               = gcc 
->    Compiler flags: 
         COMPFLAGS           = -c -DMATLAB_MEX_FILE -fcase-upper -fnounderscoring 
         OPTIMFLAGS          = -O0 
         DEBUGFLAGS          = -g 
         arguments           =  
         Name switch         = -o 
->    Pre-linking commands   =  
->    LINKER                 = C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe C:\omid\other\gnumex\linkmex.pl 
->    Link directives: 
         LINKFLAGS           =  -LC:\Omid\Other\Gnumex\libs 
         LINKDEBUGFLAGS      = -g  -Wl,--image-base,0x28000000\n 
         LINKFLAGSPOST       =  
         Name directive      = -o sodex.mexw32 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  =  
->    Resource Compiler      = C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe C:\omid\other\gnumex\rccompile.pl  -o mexversion.res 
->    Resource Linker        =  
----------------------------------------------------------------


--> gcc  -c -DMATLAB_MEX_FILE -fcase-upper -fno-underscoring -osodex.obj -O0 -DMX_COMPAT_32 sodex.f 

gcc: error: unrecognized command line option '-fcase-upper' 

  C:\PROGRA~2\MATLAB\R2011B\BIN\MEX.PL: Error: Compile of 'sodex.f' failed.  

我认为问题出在-fcase-upper选项上。我查了一下,但找不到任何重新定位gcc选项的信息。

编辑:

我在我的选项文件中手动将gcc更改为g77并成功创建了目标文件但在最后一步中仍然出现错误,我尝试将目标文件链接到主要文件名为vdpsodex.F的脚本并创建MEX文件。根据要求,我包括选项文件:

@echo off
rem C:\Omid\Other\Gnumex\mexopts_f77.bat
rem Generated by gnumex.m script in C:\omid\other\gnumex
rem gnumex version: 2.06
rem Compile and link options used for building MEX etc files with
rem the Mingw/Cygwin tools.  Options here are:
rem Gnumex, version 2.06                     
rem MinGW linking                            
rem Mex (*.dll) creation                     
rem Libraries regenerated now                
rem Language: Fortran 77                     
rem Optimization level: -O0 (no optimization)
rem StorageVersion: 1.0
rem C++keyName: GNU C++
rem C++keyManufacturer: GNU
rem C++keyLanguage: C++
rem C++keyVersion:
rem C++keyFileName: GNUMEXOPTS.bat
rem Matlab version 7.13
rem
set GCCINSTALLDIR=C:\Omid\Other\MinGW\bin
set MATLAB=C:\PROGRA~2\MATLAB\R2011b
set GM_PERLPATH=C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe
set GM_UTIL_PATH=C:\omid\other\gnumex
set PATH=C:\Omid\Other\MinGW\bin;%PATH%
set PATH=%PATH%;C:\Cygwin\usr\local\gfortran\libexec\gcc\i686-pc-cygwin\4.3.0
set LIBRARY_PATH=C:\Omid\Other\MinGW\lib
set G95_LIBRARY_PATH=C:\Omid\Other\MinGW\lib
set MW_TARGET_ARCH=win32
rem
rem precompiled library directory and library files
set GM_QLIB_NAME=C:\Omid\Other\Gnumex\libs
rem
rem directory for .def-files
set GM_DEF_PATH=C:\Omid\Other\Gnumex\libs
rem
rem Type of file to compile (mex or engine)
set GM_MEXTYPE=mex
rem
rem Language for compilation
set GM_MEXLANG=f77
rem
rem File for exporting mexFunction symbol
set GM_MEXDEF=C:\Omid\Other\Gnumex\libs\fmex.def
rem
set GM_ADD_LIBS=-lg2c -lflibmx -lflibmex -lflibmat
rem
rem compiler options; add compiler flags to compflags as desired
set NAME_OBJECT=-o
rem ************ This is where I replaced gcc with g77 ************
set COMPILER=g77
rem ***************************************************************
set COMPFLAGS=-c -DMATLAB_MEX_FILE -fcase-upper -fno-underscoring 
set OPTIMFLAGS=-O0
set DEBUGFLAGS=-g
rem
rem NB Library creation commands occur in linker scripts
rem but LIBLOC is provided for compatibility with
rem mex.getCompilerConfigurations
rem
rem Linker parameters
set LIBLOC=
set LINKER=%GM_PERLPATH% %GM_UTIL_PATH%\linkmex.pl
set LINKFLAGS=
set LINKOPTIMFLAGS=-s
set LINKDEBUGFLAGS=-g  -Wl,--image-base,0x28000000\n
set LINKFLAGS= -LC:\Omid\Other\Gnumex\libs
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=-o %OUTDIR%%MEX_NAME%.mexw32
rem
rem Resource compiler parameters
set RC_COMPILER=%GM_PERLPATH% %GM_UTIL_PATH%\rccompile.pl  -o %OUTDIR%mexversion.res
set RC_LINKER=

现在上面的代码确实给了我目标文件,但是当我尝试使用下面的命令制作最终的MEX文件时:

mex -v -f C:\Omid\Other\Gnumex\mexopts_f77.bat vdpsodex.F sodex.obj decsol.obj

它给了我以下错误:

-> Options file specified on command line 
---------------------------------------------------------------- 
->    Options file           = C:\Omid\Other\Gnumex\mexopts_f77.bat 
      MATLAB                 = C:\PROGRA~2\MATLAB\R2011b 
->    COMPILER               = g77 
->    Compiler flags: 
         COMPFLAGS           = -c -DMATLAB_MEX_FILE -fcase-upper -fno-underscoring 
         OPTIMFLAGS          = -O0 
         DEBUGFLAGS          = -g 
         arguments           =  
         Name switch         = -o 
->    Pre-linking commands   =  
->    LINKER                 = C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe C:\omid\other\gnumex\linkmex.pl 
->    Link directives: 
         LINKFLAGS           =  -LC:\Omid\Other\Gnumex\libs 
         LINKDEBUGFLAGS      = -g  -Wl,--image-base,0x28000000\n 
         LINKFLAGSPOST       =  
         Name directive      = -o vdpsodex.mexw32 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  =  
->    Resource Compiler      = C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe C:\omid\other\gnumex\rccompile.pl  -o mexversion.res 
->    Resource Linker        =  
---------------------------------------------------------------- 


--> g77  -c -DMATLAB_MEX_FILE -fcase-upper -fno-underscoring -oC:\Users\GHASEM~1\AppData\Local\Temp\2\mex_7FJjxb\vdpsodex.obj -O0 -DMX_COMPAT_32 vdpsodex.F 


--> C:\PROGRA~2\MATLAB\R2011b\sys\perl\win32\bin\perl.exe C:\omid\other\gnumex\linkmex.pl -o vdpsodex.mexw32  -LC:\Omid\Other\Gnumex\libs -s  C:\Users\GHASEM~1\AppData\Local\Temp\2\mex_7FJjxb\vdpsodex.obj  sodex.obj  decsol.obj   

C:\Omid\Other\Gnumex\libs/flibmx.lib(ds00068.o):(.idata$7+0x0): undefined reference to `_head_C__Omid_Other_Gnumex_libs_flibmx_lib' 
C:\Omid\Other\Gnumex\libs/flibmx.lib(ds00086.o):(.idata$7+0x0): undefined reference to `_head_C__Omid_Other_Gnumex_libs_flibmx_lib' 
C:\Omid\Other\Gnumex\libs/flibmx.lib(ds00062.o):(.idata$7+0x0): undefined reference to `_head_C__Omid_Other_Gnumex_libs_flibmx_lib' 
C:\Omid\Other\Gnumex\libs/flibmx.lib(ds00157.o):(.idata$7+0x0): undefined reference to `_head_C__Omid_Other_Gnumex_libs_flibmx_lib' 
C:\Omid\Other\Gnumex\libs/flibmx.lib(ds00161.o):(.idata$7+0x0): undefined reference to `_head_C__Omid_Other_Gnumex_libs_flibmx_lib' 
collect2: ld returned 1 exit status 
link command: g77 -shared C:\Omid\Other\Gnumex\libs\fmex.def -o vdpsodex.mexw32 -LC:\Omid\Other\Gnumex\libs -s C:\Users\GHASEM~1\AppData\Local\Temp\2\mex_7FJjxb\vdpsodex.obj sodex.obj decsol.obj -lg2c -lflibmx -lflibmex -lflibmat 

  C:\PROGRA~2\MATLAB\R2011B\BIN\MEX.PL: Error: Link of 'vdpsodex.mexw32' failed. 

1 个答案:

答案 0 :(得分:0)

强调文本最后,我能够指出问题,我认为对于可能面临同样问题(或类似于此问题)的其他人来说,分享答案可能会有所帮助。我只想提一下,我在第一时间看起来很奇怪,因为我使用由Gnumex创建的选项文件MEX使用完全相同的文件并且根本没有问题。我刚刚注意到,因为我之前改变了我的MinGW构建,即使我重新创建选项文件(mexopts_f77.bat),我也无法再使用相同的Fortran文件。

无论如何,长话短说我发现这个link非常有用。基本上,它建议下载五个包并将它们提取到MinGw的根目录。其中四个链接仍然存在。其中一个已经死了,但我通过谷歌搜索包名来找到它。您在下面看到的是包的更新链接。

最后但并非最不重要的是,如果您的g77不使用Gnumex制作的选项文件,则可以使用以下解决方案:

  1. 下载以下套餐:
  2. 将它们提取到MinGW根目录,例如C:\MinGW
  3. 通过选择Fortran 77作为编译语言重新制作选项文件,例如mexopt_f77.bat
  4. 使用以下语法使用Fortran脚本。

      

    mex -f [ options-file ] [fortran-file.f]

  5. 如果您需要将某些文件链接到主脚本,则必须先将它们编译为目标文件,然后将它们链接起来。

      

    mex -f [ options-file ] -c [ linkfile1.f ] [ linkfile2.f ]

         

    mex -f [ options-file ] [ fortran-file.f ] [ linkfile1.obj ] [ linkfile2。 OBJ