matlab怎么调试c,【转】Matlab中C文件的调试     Visual Studio

先普及下几个术语,让大家有一个大概框架:

a4c26d1e5885305701be709a3d33442f.png

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

Studio进行调试!

本教程演示在Visual Studio

2010中调试yprime.c文件,该文件可以到matlabroot/extern/examples/mex/目录下找到。

mex()编译生成mex二进制文件时,使用-g开关,则生成的mex文件由于依赖MATLAB某些文件,而不能直接在其他计算机的MATLAB运行,只能用于本地环境下调试。

Windows平台上调试在Visual Studio

2005/2010平台上就可以调试C/C++源码MEX文件,楼主是用的是Visual Studio

2010,下面进行图解说明

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

a4c26d1e5885305701be709a3d33442f.png

(2)直接回车,MATLAB自动搜索系统中编译器。

a4c26d1e5885305701be709a3d33442f.png

(3)楼主系统中安装两个编译器,其中Intel Visual Fortran是Fortran编译器,Visual C++

2010是C/C++编译器。而LCC-Win32是MATLAB自带的C编译器(只能编译C,不能编译C++),不是楼主安装的。输入3,回车表示选择Visual

C++ 2010作为C/C++默认编译器。

a4c26d1e5885305701be709a3d33442f.png

(4)直接回车,确定并完成您的配置。

(5)使用-g开关编译yprime.c MEX源码文件,用于后面的调试。

mex -g yprime.c

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

(8)在Attach to

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

a4c26d1e5885305701be709a3d33442f.png

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

a4c26d1e5885305701be709a3d33442f.png

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

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

yprime(1,1:4)

(12)程序会自动运行到VS中设置断点的位置,此时我们可以进行任何Visual

Studio中的调试工作,比如查内存,看地址,读数据等等。

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

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

Linux平台上调试由于楼主的Linux虚拟机上米有安装MATLAB,所以没法进行详细说明,将帮助文档中的内容复制粘贴一遍吧!

The GNU Debugger gdb, available on Linux systems, provides complete

source code debugging, including the ability to set breakpoints,

examine variables, and step through the source code

line-by-line.

In this procedure, the MATLAB command prompt

>> is shown in front of MATLAB

commands, and linux> represents a Linux prompt; your

system may show a different prompt. The debugger prompt is

.

To debug with gdb:

(1)Compile the source MEX-file with the -g option, which builds the

file with debugging symbols included. For this example, at the

Linux prompt, type:

linux> mex -g yprime.c

On a Linux 32–bit platform, this command creates

the executable file yprime.mexglx.

(2)At the Linux prompt, start the gdb debugger using the matlab

function -D option:

linux> matlab -Dgdb

(3)Start MATLAB without the Java Virtual Machine (JVM) by using the

-nojvm startup flag:

run -nojvm

(4)In MATLAB, enable debugging with the dbmex function and run your

binary MEX-file:

>> dbmex on

>> yprime(1,1:4)

(5)At this point, you are ready to start debugging.

It is often convenient to set a breakpoint at mexFunction so you

stop at the beginning of the gateway routine.

break

mexFunction

continue

(6)Once you hit one of your breakpoints, you can make full use of

any commands the debugger provides to examine variables, display

memory, or inspect registers.

To proceed from a breakpoint, type:

continue

(7)After stopping at the last breakpoint, type:

continue

yprime finishes and MATLAB displays:

ans =

2.0000 8.9685 4.0000 -1.0947

(8)From the MATLAB prompt you can return control to the debugger by

typing:

dbmex stop

Or, if you are finished running MATLAB, type:

>> quit

(9)When you are finished with the debugger, type:

quit

You return to the Linux prompt.

Refer to the documentation provided with your debugger for more

information on its use.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值