C/C++ MEX文件:也称为MEX源码文件,就是就是普通的fortran/C/C++文件,只是其中定义了一个mexFunction的接口,用于和MATLAB通讯,使用mex()函数可以将其编译为MATLAB MEX文件。至于如何书写mex源码文件,您可以看下MATLAB的帮助文档!


MATLAB MEX文件:也称为MEX二进制文件,是一种动态链接库,可以在MATLAB像普通函数一样直接调用或运行,windows平台下的扩展名为mexw32或者mexw64。使用mex()可以将MEX源码文件编译成mexw32,使用Coder工具箱可以将M文件编程mexw32。


mex()函数:MATLAB自带函数,用来将MEX源码文件编译成MEX二进制文件。

MEX函数库:在MEX源码文件中调用的MATLAB函数,主要进行和MATLAB的一些交流。

MX矩阵库:MATLAB为C/C++等提供了一些矩阵操作的库函数。


很多人都知道,使用mex()函数将C/C++源码文件编译成可以在MATLAB中直接运行的二进制mex文件。但是没有注意到还可以对生成的mex文件在Visual Studio进行调试!


Windows平台上调试

在Visual Studio 2010平台上就可以调试C/C++源码MEX文件,楼主是用的是Visual Studio 2010,下面进行图解说明

(1)选择Visual C++ 2010作为默认编译器,在MATLAB命令行输入:>> mex -setup


Welcome to mex -setup.  This utility will help you set up a default compiler.



For a list of supported compilers, see


http://www.mathworks.com/support/compilers/R2012a/win32.html

(2)直接回车,MATLAB自动搜索系统中编译器。Please choose your compiler for building MEX-files:

Would you like mex to locate installed compilers [y]/n?


Select a compiler:



[1] Intel Visual Fortran 12.0 (with Microsoft Visual C++ 2010 linker) in C:\Program Files\Intel\ComposerXE-2011\


[2] Lcc-win32 C 2.4.1 in D:\Program\MATLAB\R2012a\sys\lcc


[3] Microsoft Visual C++ 2010 in D:\Program\Visual Studio


[0] None

(3)楼主系统中安装两个编译器,其中Intel Visual Fortran是Fortran编译器,Visual C++ 2010是C/C++编译器。而LCC-Win32是MATLAB自带的C编译器(只能编译C,不能编译C++),不是楼主安装的。

输入3,回车表示选择Visual C++ 2010作为C/C++默认编译器。Compiler: 3


Please verify your choices:



Compiler: Microsoft Visual C++ 2010   Location: D:\Program\Visual Studio

(4)直接回车,确定并完成您的配置。Are these correct [y]/n?

*************************************************************************** Warning: MEX-files generated using Microsoft Visual C++ 2010 require            that Microsoft Visual Studio 2010 run-time libraries be            available on the computer they are run on.

If you plan to redistribute your MEX-files to other MATLAB             users, be sure that they have the run-time libraries.   ***************************************************************************

Trying to update options file:

C:\Users\Dynamic\AppData\Roaming\MathWorks\MATLAB\R2012a\mexopts.bat From

template:

D:\Program\MATLAB\R2012a\bin\win32\mexopts\msvc100opts.bat

Done . . .

**************************************************************************

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/techdoc/matlab_external/bsflnue-1.html              Building with the -largeArrayDims option enables the new API.

**************************************************************************

(5)使用-g开关编译yprime.c MEX源码文件,用于后面的调试。mex -g yprime.c 此时当前目录下会生成一个yprime.mexw32文件,楼主的计算机是32位的,如果您是64位的操作系,那将生成一个yprime.mexw64的文件。

(6)启动Visual Studio,注意不要退出MATLAB,否则您会找不到相关进程的。

(7)在Visual Studio中进行菜单操作,Tools→Attach to Process…

(8)在Attach to Process对话框中,选择MATLAB进程,点击“Attach(A)”按钮。

(9)此时VS会自动加载所有相关数据,菜单操作,File→Open→File…选择MATLAB当前工作目录中的yprime.c文件。

(10)您可以在yprime.c中任意设置断点,一般我们喜欢在mexFunction的开始位置就给一个断点。

(11)在MATLAB中运行MEX二进制文件,比如输入:yprime(1,1:4)

(12)程序会自动运行到VS中设置断点的位置,此时我们可以进行任何Visual Studio中的调试工作,比如查内存,看地址,读数据等等。

(13)点击菜单操作,Debug→Contiue,将程序执行完毕,MATLAB

命令窗口会显示如


下内容:


>> yprime(1,1:4)

ans =

2.0000

8.9685

4.0000

-1.0947


注意

:如果您想将生成的mexw32文件发布给第三方,请重新使用mex()函数进行编译,使用-g开关生成的mexw32文件只能用于调试!

转载于:https://www.cnblogs.com/trace617/p/5065718.html