Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3309681
  • 博文数量: 754
  • 博客积分: 10132
  • 博客等级: 上将
  • 技术积分: 7780
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-14 23:36
文章分类

全部博文(754)

文章存档

2012年(3)

2011年(39)

2010年(66)

2009年(167)

2008年(479)

我的朋友

分类: C/C++

2010-07-13 17:21:00

MATLAB下调试C/C++程序

    如果你有一个C/C++的函数需要测试,利用MATLAB平台是一个经济高效的选择。你不必花过多的精力去理会IO的问 题,并且可以很方便的利用 MATLAB的函数来验证你的函数的正确性。有时,你还可 以利用MATLAB产生测试数据。这样做还有一个很大的好处,你不必等到整个程序写完了才进行测试。每写完一个小函 数,就进行调试和测试,确保其顺利运行和得到正确的输出值,特别是对科学计算函数,你必须保证你的函数的计算结果是在要求的精度范围内的。

第一个简单的例子是编写一个定点运算的sin函数,通过mex编 译成dll形式的mex文件,然后就可以在MATLAB下像普通的m函数一 样调用了。

/**//*

* Example1.cpp

* A fix point sin function, which accepts angle rather radian.

*/

static int SIN_LUT[91] = { 

       0, 571, 1143, 1714, 2285, 

       2855, 3425, 3993, 4560, 5126, 

       5690, 6252, 6812, 7371, 7927, 

       8480, 9032, 9580, 10125, 10668, 

       11207, 11743, 12275, 12803, 13327, 

       13848, 14364, 14876, 15383, 15886, 

       16383, 16876, 17364, 17846, 18323, 

       18794, 19260, 19720, 20173, 20621, 

       21062, 21497, 21926, 22347, 22762, 

       23170, 23571, 23964, 24351, 24730, 

       25101, 25465, 25821, 26169, 26509, 

       26841, 27165, 27481, 27788, 28087, 

       28377, 28659, 28932, 29196, 29451, 

       29697, 29935, 30163, 30381, 30591, 

       30791, 30982, 31164, 31336, 31498, 

       31651, 31794, 31928, 32051, 32165, 

       32270, 32364, 32449, 32523, 32588, 

       32643, 32688, 32723, 32748, 32763, 

       32768

};

 

int mlsak_sin(int angle)

{

       int sign=1;

 

       if ( angle<0 )

       {

              angle=-angle;

              sign = -1;

       }

 

       angle %= 360;

 

       switch (angle/90)

       {

              case 0:

                     return sign*SIN_LUT[angle];

              case 1:

                     return sign*SIN_LUT[180-angle];

              case 2:

                     return -sign*SIN_LUT[angle-180];

              case 3:

                     return -sign*SIN_LUT[360-angle];

              default:

                     return SIN_LUT[91]+1;

       }

}

// -------------------------------------------------------------------------

// for test sin in MATLAB

// sinv = mysin(angle)

// -------------------------------------------------------------------------

//

#ifdef TEST_MATLAB_TRIGON_SIN

#include "mex.h"

 

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

{

       int angle;

       double *sinv;

 

       angle = (int)mxGetScalar(prhs[0]);

       plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);

       sinv = (double*) mxGetData(plhs[0]);

 

       *sinv = (double) mlsak_sin(angle);

 

       return;

}

#endif//!TEST_MATLAB_TRIGON_SIN

 

mex.h/extern/include/,该文件又包含了matrix.h文件,在前者中declare的函数名皆以mex开 头,在后者中申明的函数名皆以mx开头,具体可以查阅帮助。

上面这个文件写好后,就可以编译了。编译前有一些准 备工作要做。

STEP1. MATLAB下运行mex -setup,当然如果以前运行过而且现在又不想指定别的C/C++编译器,就跳过。设置很简单,step by step,就是指定编译 器,我这里指定的是VC 6.0

STEP2. MATLAB下或cmd中运行mex -DTEST_MATLAB_TRIGON_SIN Example1.cpp -output mysin. 这样在当前目录下你就会看见一个mysin.dll的文件(注意前面不要加.dllMATLAB会自动加上)。如果你 是要一个调试版本,就在命令行中加上-g开关。如果你对C/C++编 译器还有些参数要指定,可以将 /bin/win32/mexopts/msvc60opts.bat拷贝到当前目录下修改之,再加上-f 就行了。

mex -g -DTEST_MATLAB_TRIGON_SIN Example1.cpp -output mysin

-g

          Create a debuggable MEX-file. If this option is specified, MEX appends

          the value of options file variables ending in DEBUGFLAGS with their

          corresponding base variable. (For example, the value of LINKDEBUGFLAGS

          would be appended to the LINKFLAGS variable before calling the

          linker.) This option also disables MEX's default behavior of

          optimizing built object code.

-D

          Define a symbol name to the C preprocessor. Equivalent to a

          "#define " directive in the source.

-D#

          Define a symbol name and value to the C preprocessor. Equivalent to a

          "#define " directive in the source.

STEP3. 现在已经可以在MATLAB下调用mysin了,当然得注意要让MATLAB找得着这个文件。还可以在mysin.dll的同一个目录下写一个mysin.m文件,提供帮助信息。如果想能够进行调试,就转入下一步。

STEP4. cmd下键入msdev mysin.dllmsdev.exe VC进程。现在,vc打开了,打开你要调试的C/C++文 件,这个例子中就是Example1.cpp,设置好断点,按Alt+F7,在 Debug表单的Excuitable for debug session中键入$MATLABBOOT/bin/win32/matlab.exe就行了。按F5MATLAB就被打开,好,可以工作了。

STEP4. 不一定如上执行。可以在VC中用Compile Ctrl+F7 编译无错后,在MATLAB中编写测试程序,本例中,测试m文件如下:

for i=1:360*2 ;  mysin.c中形参是int 型。

  a(i)=mysin(i);  %注意:测试文件名不能为mysin.m,但函数调用时文件名应为mysin

               %而不应该时测试文件名。因为调用的是mysin.dll文件。

end;

plot(a);

结果如图:  

    下面解释一下mex的入口函数,mexFunction,它就好比C/C++中 的main函数一样。mxArraymatrix.h中定义的 struct结构类型,对于它的操作有相应的接口函数,不用深究其结构。mexFunction4个参数,分别对应于命令的返回值 和输入参数,在这个例子中,mysin接受一个参数,angle, 一个返回值,sinvlhs,是left hand side的缩写,因为返回值在命令的左边,rhsright hand side的缩写,因为输入参数在命令的右边。nlhs是 返回值的个数,plhs是返回值数组,nrhs是 输入参数个数,prhs是输入参数数组。

阅读(5856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~