注:本篇文章用到的是VS2008 和 matlab2009a,其实本人用matlab2013也可运行。
一:matlab配置
首先,我们启动matlab,然后按照下面的步骤来配置初始化环境:(英文为matlab中显示的内容,红色部分是输入)
mbuild-setup
Please choose yourcompiler for buildingstandalone MATLAB applications:
Would you like mbuild to locate installedcompilers [y]/n?y
Select a compiler:
[1] Lcc-win32 C2.4.1 inC:\PROGRA~1\MATLAB\R2009a\sys\lcc
[0] None
Compiler: 0
mbuild: No compiler selected. Noactiontaken.
mbuild -setup (这里一定注意中间的空格)
Please choose yourcompiler forbuilding standalone MATLAB applications:
Would you like mbuildto locateinstalled compilers [y]/n?n
Select a compiler:
[1] Lcc-win32 C 2.4.1
[2] Microsoft VisualC++ 6.0
[3] Microsoft VisualC++ .NET 2003
[4] Microsoft VisualC++ 2005 SP1
[5] Microsoft VisualC++ 2008Express
[6] Microsoft VisualC++ 2008 SP1
[0] None
Compiler:6
The default locationfor Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft VisualStudio9.0,
butthat directory does not existon this machine.Use C:\ProgramFiles\MicrosoftVisual Studio 9.0 anyway [y]/n? y
Please verify yourchoices:
Compiler: MicrosoftVisual C++ 2008
Location:C:\ProgramFiles\Microsoft Visual Studio 9.0
Are these correct[y]/n?y
Warning:Applications/components generated using MicrosoftVisualC++
2008 require that the MicrosoftVisual Studio 2008run-time
libraries be available on thecomputer used fordeployment.
To redistribute yourapplications/components, be sure that the
deployment machine has theserun-time libraries.Trying to updateoptions file:C:\Documents andSettings\Administrator\ApplicationData\MathWorks\MATLAB\R2009a\compopts.bat
Fromtemplate: C:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc90freecompp.bat
Done . . .
function y = add(a,b)
y = a + b;
end
然后 在matlab命令行里输入mcc –W cpplib:myadd –T link:lib add.m –C
运行后等待一段时间,你将会在当前的目录下发现多了几个文件,如下图:
注意:myadd.ctf, myadd.h, myadd.lib, myadd.dll 这四个文件将会在VS2008项目中用到,你必须把它们复制到你当前的VS项目目录下。
二:VS2008配置
创建一个基于对话框的MFC项目,命名为myaddTest, 然后按下面的步骤配置“include”和“lib”
选择 工具—选项 然后加入matlab的include目录,具体如下图:
选择 工具—选项 然后增加matlab的lib,具体如下图:
然后右击你的项目,选择属性—链接器—输入,按下图输入:
这样myadd.lib就被创建,而mclmcrrt.lib对于myadd.dll来说也是必要的。
三:基本函数的实现
打开你的myaddTest项目,增加以下空间和按钮,可以成下图所示:
双击计算按钮,修改函数void CMyAddTestDlg::OnBnClickedOk(),如下:
1. void CMyAddTestDlg::OnBnClickedOk()
2. {
3. // TODO: 在此添加控件通知处理程序代码
4. CString str;
5. int a,b,c;
6.
7. //get what’s in the edit control and save to a,b
8. a=GetDlgItemInt(IDC_EDIT1);
9. b=GetDlgItemInt(IDC_EDIT2);
10.
11. //initializing function to avoid failure
12. if(!myaddInitialize())
13. {
14. str.Format(_T("lib myadd2 Initialize failed!\n"));
15. MessageBox(str);
16. return;
17. }
18. try
19. {
20. //declare a mwArray variable and store a to it
21. mwArray mwa(1,1,mxUINT32_CLASS);
22. mwa.SetData(&a,1);
23.
24. //declare a mwArray variable and store b to it
25. mwArray mwb(1,1,mxUINT32_CLASS);
26. mwb.SetData(&b,1);
27.
28. //declare a mwArray variable and store the result
29. mwArray mwy(1,1,mxUINT32_CLASS);
30.
31. //call the add function and store mwy to c
32. add(1,mwy,mwa,mwb);
33. mwy.GetData(&c,1);
34.
35. SetDlgItemInt(IDC_EDIT3,c);
36. }
37. catch(mwException&e)
38. {
39. str.Format(_T("%s"),e.what());
40. MessageBox(str);
41. }
42.
43. //the ending function
44. myaddTerminate();
45.
46. //OnOK();youshould add an annotation of it
注意:不要忘了在 MyAddTestDlg.cpp 加上头文件myadd.h.
运行,出现如下结果: