在 Matlab 中调试用 C/C++编写的 MEX 文件(32、64 位机,VS2005 和 matlab
R2010b)
mex 文件是 matlab 提供的与外部语言如 C、C++提供的接口函数。可以在
Matlab 环境下编写一个*.m 文件,来编译所定的 C/C++文件,当然,这个 C/C+
+文件的开头是有要求的。Mex 函数有固定的自己的一套接口形式
以 VS2005 和 matlab R2010b 7.11.0 版本为例,C/C++文件在 matlab 编译格
式为*.mexw32,准备好你的 C/C++文件后:
[1]、matlab 安装 mex 编译工俱
命令如: mex –setup
编译工具选择:VS2005;一路 yes 下来。最后,只要不报错就可以。
[2]、在 matab 环境中,编写编译文件 compile_mex .m
把 C/C++文件编译成*.mexw32 文件时,要使用 mex –g 命令,产生具有调
试 信 息 的 执 行 文 件 。 而 不 是 mex –o 命 令 , 后 者 是 不 含 调 试 信 息 的 。
compile_mex.m 文件内容如下;
function compile_mex(debug_flag)
if strfind(computer(),'64')
defs = '-DA64BITS '; % for 64bit machines - define pointer type
else
defs = '';
end
if verLessThan('matlab','7.3')
defs = [defs, '-DmwIndex=int -DmwSize=size_t '];
end
if nargin>0 && debug_flag
debugs = ' -g ';
sprintf('mex -g');
else
debugs = ' -O ';
end
cmd = sprintf('mex %s -largeArrayDims %s filename.cpp ', debugs,
defs);
eval(cmd);
在命令行执行:
debug_flag =’1’, compile_mex(debug_flag)
评论3