Matlab MEX 使用笔记

来源:互联网 发布:淘宝固定代码 编辑:程序博客网 时间:2024/05/19 00:50

Matlab 中使用 C/C++ 或 Fortran 代码的一种方式是利用 mex 进行编译,然后调用。


1 编译器设置

[python] view plain copy print?
  1. mex -setup  

有可能见到的信息是这个样子:

[python] view plain copy print?
  1. >> mex  -setup  
  2.    
  3. Welcome to mex -setup.  This utility will help you set up    
  4. a default compiler.  For a list of supported compilers, see    
  5. http://www.mathworks.com/support/compilers/R2012b/win64.html   
  6.    
  7. Please choose your compiler for building MEX-files:   
  8.    
  9. Would you like mex to locate installed compilers [y]/n? y  
  10.    
  11. No supported SDK or compiler was found on this computer.   
  12. For a list of supported compilers, see    
  13. http://www.mathworks.com/support/compilers/R2012b/win64.html    
  14.    
  15. Error using mex (line 206)  
  16. Unable to complete successfully.  

Matlab 无法完成自动搜索编译器,可以选择 n 再尝试。
[python] view plain copy print?
  1. Welcome to mex -setup.  This utility will help you set up    
  2. a default compiler.  For a list of supported compilers, see    
  3. http://www.mathworks.com/support/compilers/R2012b/win64.html   
  4.    
  5. Please choose your compiler for building MEX-files:   
  6.    
  7. Would you like mex to locate installed compilers [y]/n? n  
  8.    
  9. Select a compiler:   
  10. [1] Intel C++ 12.0 (with Microsoft Software Development Kit (SDK) linker)   
  11. [2] Intel C++ 12.0 (with Microsoft Visual C++ 2008 SP1 linker)   
  12. [3] Intel C++ 12.0 (with Microsoft Visual C++ 2010 linker)   
  13. [4] Intel C++ 11.1 (with Microsoft Visual C++ 2008 SP1 linker)   
  14. [5] Intel Visual Fortran 12 (with Microsoft Software Development Kit (SDK) linker)   
  15. [6] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 SP1 linker)   
  16. [7] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2008 Shell linker)   
  17. [8] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2010 linker)   
  18. [9] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 SP1 linker)   
  19. [10] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 Shell linker)   
  20. [11] Microsoft Software Development Kit (SDK) 7.1   
  21. [12] Microsoft Visual C++ 2005 SP1   
  22. [13] Microsoft Visual C++ 2008 SP1   
  23. [14] Microsoft Visual C++ 2010   
  24.    
  25. [0None   

比较糟糕,无法匹配,用的是 2013 版的 Intel Fortran。可能需要自己编译了。


2 编译文件

[python] view plain copy print?
  1. mex xxx.c  
  2.   
  3. mex xxx.f90  


[python] view plain copy print?
  1. mex -v COMPFLAGS='/fpp /Qprec "/ID:\PROGS\MATLAB\R2010A/extern/include" -c -nologo -DMATLAB_MEX_FILE /fixed /fp:source /assume:bscc /libs:static /thread' LDFLAGS='$LDFLAGS -mt' supcrt.f supcrt92.f  


[python] view plain copy print?
  1. >> mex -v supcrt.f supcrt92.f  
  2.  This is mex, Copyright 1984-2007 The MathWorks, Inc.   
  3.    
  4. -> Default options filename found in C:\Documents and Settings\hu\Application Data\MathWorks\MATLAB\R2010a   
  5. ----------------------------------------------------------------   
  6. ->    Options file           = C:\Documents and Settings\hu\Application Data\MathWorks\MATLAB\R2010a\mexopts.bat   
  7.       MATLAB                 = D:\PROGS\MATLAB\R2010A   
  8. ->    COMPILER               = ifort   
  9. ->    Compiler flags:   
  10.          COMPFLAGS           = /fpp /Qprec "/ID:\PROGS\MATLAB\R2010A/extern/include" -c -nologo -DMATLAB_MEX_FILE /fixed /MD /fp:source /assume:bscc   
  11.          OPTIMFLAGS          = /O2 /DNDEBUG   
  12.          DEBUGFLAGS          = /Z7   
  13.          arguments           =    
  14.          Name switch         = /Fo   
  15. ->    Pre-linking commands   =    
  16. ->    LINKER                 = link   
  17. ->    Link directives:   
  18.          LINKFLAGS           = /dll /export:MEXFUNCTION /LIBPATH:"D:\PROGS\MATLAB\R2010A\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:"F:\TEMP\MEX_VQ~1\templib.x" /MAP:"supcrt.mexw32.map" /NOLOGO /INCREMENTAL:NO   
  19.          LINKDEBUGFLAGS      = /debug /PDB:"supcrt.mexw32.pdb"   
  20.          LINKFLAGSPOST       =    
  21.          Name directive      = /out:"supcrt.mexw32"   
  22.          File link directive =    
  23.          Lib. link directive =    
  24.          Rsp file indicator  = @   
  25. ->    Resource Compiler      = rc /fo "mexversion.res"   
  26. ->    Resource Linker        =    
  27. ----------------------------------------------------------------   
  28.    
  29.    
  30. --> ifort  /fpp /Qprec "/ID:\PROGS\MATLAB\R2010A/extern/include" -c -nologo -DMATLAB_MEX_FILE /fixed /MD /fp:source /assume:bscc /FoF:\TEMP\MEX_VQ~1\supcrt.obj /O2 /DNDEBUG -DMX_COMPAT_32 supcrt.f   
  31.    
  32. ifort: warning #10268: Microsoft compiler version 6.0 or earlier is not supported   
  33.    
  34. --> ifort  /fpp /Qprec "/ID:\PROGS\MATLAB\R2010A/extern/include" -c -nologo -DMATLAB_MEX_FILE /fixed /MD /fp:source /assume:bscc /FoF:\TEMP\MEX_VQ~1\supcrt92.obj /O2 /DNDEBUG -DMX_COMPAT_32 supcrt92.f   
  35.    
  36. ifort: warning #10268: Microsoft compiler version 6.0 or earlier is not supported   
  37. supcrt92.f(664): warning #6375: Because of COMMON, the alignment of object is inconsistent with its type   [ZPRTR]   
  38.       COMMON /refval/ mwH2O, R, Pref, Tref, ZPrTr, YPrTr   
  39. --------------------------------------------^   
  40. supcrt92.f(664): warning #6375: Because of COMMON, the alignment of object is inconsistent with its type   [YPRTR]   
  41.       COMMON /refval/ mwH2O, R, Pref, Tref, ZPrTr, YPrTr   
  42. ---------------------------------------------------^   
  43.     Contents of F:\TEMP\MEX_VQ~1\mex_tmp.rsp:   
  44.   F:\TEMP\MEX_VQ~1\supcrt.obj  F:\TEMP\MEX_VQ~1\supcrt92.obj   
  45.    
  46.    
  47. --> link /out:"supcrt.mexw32" /dll /export:MEXFUNCTION /LIBPATH:"D:\PROGS\MATLAB\R2010A\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:"F:\TEMP\MEX_VQ~1\templib.x" /MAP:"supcrt.mexw32.map" /NOLOGO /INCREMENTAL:NO  @F:\TEMP\MEX_VQ~1\MEX_TMP.RSP     
  48.    
  49.    Creating library F:\TEMP\MEX_VQ~1\templib.x and object F:\TEMP\MEX_VQ~1\templib.exp   
  50.    
  51. --> del "F:\TEMP\MEX_VQ~1\templib.x" "F:\TEMP\MEX_VQ~1\templib.exp"   
  52.    
  53.    
  54. --> mt -outputresource:"supcrt.mexw32";2 -manifest "supcrt.mexw32.manifest"   
  55.    
  56. Microsoft (R) Manifest Tool version 5.2.3790.2075    
  57. Copyright (c) Microsoft Corporation 2005.     
  58. All rights reserved.    
  59.    
  60. --> del "supcrt.mexw32.manifest"   
  61.    
  62.    
  63. --> del "supcrt.mexw32.map"   




3 调用


很简单,直接调用即可。


[python] view plain copy print?
  1. xxx  


问题总结:


主要问题在于编译器的设置,需要安装特定版本的C/C++编译器和Fortran编译器,考虑自己编写脚本或CMake来编译。


实际上,mex文件就是一个导出了 mexFunction 函数的动态链接库文件,按DLL方式编译,加上导出信息即可。


[python] view plain copy print?
  1. /export:MEXFUNCTION  


0 0
原创粉丝点击