VC调用Matlab测试
- 格式:pdf
- 大小:363.54 KB
- 文档页数:8
VC调⽤Matlab测试
1.MCC编译法(EXE)
将matlab的m⽂件编译成VC++能执⾏的cpp⽂件。1.1编译器安装
>mbuild -setup
选择matlab的默认编译器就可以。1.2mcc⽣成exe⽂件测试
matlab的tt.m⽂件代码:
t=0:pi/20:2*pi;
[x,y]=meshgrid(t);
subplot(2,2,1);
plot(sin(t),cos(t));
axis equal;
subplot(2,2,2);
z=sin(x)+sin(y);
plot(t,z);
%axis([0 2*pi-2 2]);
subplot(2,2,3);
z=sin(x).*cos(y);
plot(t,z);
%axis([0 2*pi-1 1]);
subplot(2,2,4);
z=(sin(x).^2)-(cos(y).^2);
plot(t,z);
%axis([0 2*pi-1 1]);
⽣成的图如下所⽰:
1.3遇到问题
1)利⽤mcc –m tt.m编译后会⽣成如下⽂件:
双击tt.exe会出现⿊屏并⼀闪⽽过,不⽅便我们看出错的代码,所以在DOS环境中运⾏,如下:
解决⽅法:安装MCRInstaller.exe,我的在G:\Matlab2009a\toolbox\compiler\deploy\win32路径下。2)消除运⾏MATLAB⽣成的exe程序的dos⿊屏
基于MATLAB⽣成exe⽂件后,每次运⾏都存在dos⿊屏的问题,现在可以通过以下
⽅法解决:
在matlab命令窗⼝输⼊:cd(prefdir)
edit compopts.bat
在打开的⽂件最后添加以下语句:A.VC环境下:
set LINKFLAGS=%LINKFLAGS% /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
B.LCC环境下:set LINKFLAGS=%LINKFLAGS% -subsystem windows
C. Borland:
set LINKFLAGS=%LINKFLAGS% -aa
保存以后,再重新编译m⽂件,⽣成的exe⽂件运⾏起来就没有dos窗⼝了。3)将exe程序拷到别⼈机⼦上显⽰找不到mclmcrrt710.dll
需要将⾃⼰matlab的MCRInstaller.exe在别⼈的机⼦上安装,⽽不能安装别⼈机⼦的MCRInstaller.exe。4)双击exe出现Runtime Error!R6034
An application has made an attempt to load the C runtime library incorrectly. Please contact the application's support team formore information.
解决⽅法:Create a manifest file named application_name.exe.manifest (if creating an executable) orapplication_name.dll.manifest (if creating a shared library) with the following content:
1.4后续问题
1)没有测试如果有参数的话怎么传递?
2.MCC编译法(DLL)
2.1基本配置(引⽤⽹上的)
新建⼀个win32 控制台应⽤程序,我取的名字是matlabDll2.当然新建其他的项⽬类型也可以,我这只是个例⼦。接下来进⾏配置,在该项⽬的属性中进⾏了配置,只对该项⽬有效。若建新的项⽬需要重新配置。项⽬建好后将libmyadd2.lib,libmyadd2.h, libmyadd2.dll拷贝到项⽬⽬录下。
⾸先配置项⽬属性页/配置属性/C-C++/常规/附加包含⽬录,请根据⾃⼰电脑上软件的安装位置对照设置,2008与2005不同的地⽅时这⾥要加两个⽬录,如下图所⽰:
其次配置项⽬属性页/配置属性/链接器/常规/附加库⽬录,请根据⾃⼰电脑上软件的安装位置对照设置,如下图所⽰:
然后配置项⽬属性页/配置属性/链接器/输⼊/附加依赖性,填⼊libmyadd2.lib mclmcrrt.lib mclmcr.lib ,如下图所⽰:
在这⼀步俺可吃了⼤苦头了,有篇⽂档只说添加前两项,俺就照做了,结果导致运⾏失败,找了好长时间也没发现错误,两天的功夫都在找,结果就是因为少填了这⼀项,还有其他的⼀些包含lib,⽐如libmex.lib libmx.lib ,这⾥没填,好像是VC6上要配的。2.2 ⼀些错误和解决⽅法
1)编译命令mcc -W cpplib:libtt -T link:lib tt.m
tt函数带了两个控制t范围的参数。
mcc -W cpplib:libtt1 -T link:lib tt.m
Error libtt1.cpp: 147 syntax error; found `void' expecting `;'
Error libtt1.cpp: 147 syntax error; found `&' expecting `)'
Error libtt1.cpp: 147 skipping `&' `start' `,'
Error libtt1.cpp: 147 syntax error; found `const' expecting `{'
Error libtt1.cpp: 147 redeclaration of `mwArray' previously declared at libtt1.cpp 147
Error libtt1.cpp: 147 syntax error; found `&' expecting `;'
Error libtt1.cpp: 147 undeclared identifier `end1'
Error libtt1.cpp: 147 syntax error; found `)' expecting `;'
Error libtt1.cpp: 147 illegal statement termination
Error libtt1.cpp: 147 skipping `)'
Error libtt1.cpp: 149 undeclared identifier `start'Warning libtt1.cpp: 147 local `const int mwArray' is not referenced
Error libtt1.cpp: 150 syntax error; found `end of input' expecting `}'
12 errors, 1 warnings
G:\MA TLAB~1\BIN\MEX.PL: Error: Compile of 'libtt1.cpp' failed.
Error: An error occurred while shelling out to mbuild (error code = 9).
Unable to build executable (specify the -v option for more information).
Error using ==> mcc
Error executing mcc, return status = 1 (0x1).
这是因为我⽤的是Matlab内置的编译器,⽽它内置的编译器可以编译为C的但是不能编译为CPP的。重新设置编译器就好了。2)编译链接均⽆错误信息,运⾏后显⽰:
Cannot open CTF archive file
‘C\document~1\PC_name\locals~1\temp\mathworks_tmp_4012_22132'
or
‘C\document~1\PC_name\locals~1\temp\mathworks_tmp_4012_22132.zip' Undefined funciton or variable 'matlabrc'
这个问题很纠结,在⽹上也查了很久,但是我出现这个问题不是没安装MCRinstaller什么的,也不是⽣成的那些东西没放到相应的⽂件夹中,⽽是我在VS2008⾥⾯建⽂件的时候⽤的是添加现有项,这个最好⾃⼰新建⼀个。弄好过后就有:
3)这个调⽤的main代码
#include"libtt.h"
#include"mclmcr.h"
#include"mclcppclass.h"
int main()
{
int var;
if( !mclInitializeApplication(NULL,0) )
{
std::cout << "Could not initialize the application!" << std::endl;
return -1;
}
if( !libttInitialize())
{
std::cout << "Could not initialize libtt!" << std::endl;
return -1;}
mwArray e(1,1,mxDOUBLE_CLASS,mxREAL);
e(1,1)=6.14159;
mwArray s(1,1,mxDOUBLE_CLASS,mxREAL);
s(1,1)=0;
tt(0,s,s,e);
std::cin>>var;
libttTerminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
注意三点:
第⼀,tt函数的原型如下:LIB_libtt_CPP_API
void MW_CALL_CONV tt(int nargout, mwArray& r,const mwArray& start, const mwArray& end1)
{
mclcppMlfFeval(_mcr_inst, "tt", nargout, 1, 2,&r,&start, &end1);
}
第⼆,需要加std::cin>>var;否者图像就会⼀闪⽽过。
第三,在debug⽣成的exe⾥⾯还需要加上.ctf⽂件,否则也会报错,这个⽂件在mcc时加上-C选项可以⽣成。
结果如下图,可以看出坐标范围发⽣了变化: