Matlab 与 VC++ 混合编程过程详解(含问题部分解答)
--by 孤鸿原野
本文以一个简单的函数为例为例,说明Matlab与VC++6.0的混合编程过程。
Matlab版本:Matlab R2009a
VC++版本:VC++6.0 SP3企业版
操作系统:Window 7 旗舰版
1. 将需要执行代码在Matlab 中编写.m文件,如编写myadd.m:
function myadd(a, b)
% dummy function, just to demonstrate the idea
y = a+b;
z = a+2*b;
end
2. 在Matlab中将.m文件编译成.dll(动态链接库),供VC++调用。步骤如下(红色文字为用户输入内容,注意空格)
(1) 设置Matlab编译环境:
在Matlab命令窗口输入:mbuild –setup按回车,出现:
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n?
在选项处输入y回车,出现:
Select a compiler:
[1]Lcc-win32C 2.4.1 in D:/PROGRA~1/MATLAB/R2009a/sys/lcc
[2] Microsoft Visual C++ 6.0 in C:/Program Files/Microsoft Visual Studio
[0] None
Compiler:
输入选择项2回车,出现:
Please verify your choices:
Compiler: Microsoft Visual C++ 6.0
Location: C:/Program Files/Microsoft Visual Studio
Are these correct [y]/n?
输入y回车,出现
Trying to update options file: C:/Users/Administrator/AppData/Roaming/MathWorks/MATLAB/R2009a/compopts.bat
From template: D:/PROGRA~1/MATLAB/R2009a/bin/win32/mbuildopts/msvc60compp.bat
Done . . .
继续输入mex –setup并回车,出现:
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n?
输入y回车,出现:
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:/PROGRA~1/MATLAB/R2009a/sys/lcc
[2] Microsoft Visual C++ 6.0 in C:/Program Files/Microsoft Visual Studio
[0] None
Compiler:
输入选项2回车,出现:
Please verify your choices:
Compiler: Microsoft Visual C++ 6.0
Location: C:/Program Files/Microsoft Visual Studio
Are these correct [y]/n?
输入y回车,出现:
Trying to update options file: C:/Users/Administrator/AppData/Roaming/MathWorks/MATLAB/R2009a/mexopts.bat
From template: D:/PROGRA~1/MATLAB/R2009a/bin/win32/mexopts/msvc60opts.bat
Done . . .
******************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/support/solutions/data/1-5C27B9.html?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
******************************************************
至此Matlab编译环境设置成功。
(2) 将步骤1中的.m文件保存到Matlab当前目录,一般为
D:/My Documents/MATLAB
在Matlab窗口中输入:
mcc -t -h -L C -W lib:××× -T link:lib ×××.m
回车,等待编译完成。×××为.m文件文件名,一般也是编写的函数名。本例中,应在命令窗口输入:
mcc -t -h -L C -W lib: myadd -T link:lib myadd.m
3. 新建VC++ MFC App项目,本例中项目名为test,并配置VC++6.0编译器环境。具体过程如下:
打开VC++6.0中“工具(Tool)”->“选项” (Options)->“目录”(Directory)选项页:
在“目录(Directory(S))”下拉菜单中选择Include files,并在“路径(D)”中添加Matlab安装目录中的/extern/include,例如本例中加入:
D:/PROGRAM FILES/MATLAB/R2009A/EXTERN/INCLUDE
在“目录(Directory(S))”下拉菜单中选择Library files,并在“路径(D)”中添加Matlab安装目录中的/extern/lib/win32/microsoft,
在本例中应加入:
D:/PROGRAM FILES/MATLAB/R2009A/EXTERN/LIB/WIN32/MICROSOFT
4. 将步骤2中生成的myadd.dll、myadd.h和myadd.lib文件复制到 VC++工程所在文件夹下。
5. 打开VC++ “工程(Project(P))”->“设置(Setting(S))”->“连接(link)”选项卡,在“对象/库模块(object/library modules)”栏中加入:
×××.lib mclmcrrt.lib mclmcr.lib
6. 编写VC++ 工程代码,在需要调用.m文件的按键实现文件(.cpp)头部加入“#include myadd.h”语句。以下是test项目下
“确定”按键的代码,该按键用来进行调用myadd.m文件,实现两个double变量的相加。
代码:
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE); //数据更新
bool result=myaddInitialize(); //初始化
if(result==FALSE)
{
MessageBox(LPCTSTR(L"初始化失败,请重新点击该按键!"),LPCTSTR(L"提示信息:"),MB_OK|MB_ICONINFORMATION);
}
double para1[1]={2}; //参数1
double para2[1]={3}; //参数2
mxArray *Para1 = mxCreateDoubleMatrix(1,1,mxREAL);
memcpy(mxGetPr(Para1),para1,sizeof(double));
mxArray *Para2 = mxCreateDoubleMatrix(1,1,mxREAL);
memcpy(mxGetPr(Para2),para2,sizeof(double));
//参数传递已经完成,销毁所有参数矩阵
mxDestroyArray(Para1);
mxDestroyArray(Para2);
}
部分问题解答:
1、 出现error C2065: ' mxCreateDoubleMatrix ' : undeclared identifier
解答:可能是Matlab版本较低,请尝试mlfDoubleMatrix()函数创建矩阵变量
2、 出现:
LINK : fatal error LNK1104: cannot open file "×××.lib"
Error executing link.exe.
解答: 可能是步骤3和步骤5没有做。请自信检查VC++的编译环境的配置。若配置没有问题,则可能是Matlab版本的问题。在低级Matlab下步骤5 的配置文件应改成:
×××.lib libmx.lib libmatlb.lib libmmfile.lib
3、 无法生成,或点击生成 按钮没有响应。
解答: 可能是操作系统原因,Window7对VC++6.0的兼容性较差,建议在Window7下点击“组建(B)”->“清除”和“组建(B)”->“全部重建”来编译和生成可执行程序。
4、 若C++开发环境是VS2008,则步骤2中有所改动,具体步骤如下(红色字体为用户输入)(在Matlab命令窗口中进行):
>> mbuild -setup
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? n
Select a compiler:
[1] Lcc-win32 C 2.4.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ .NET 2003
[4] Microsoft Visual C++ 2005 SP1
[5] Microsoft Visual C++ 2008 Express
[6] Microsoft Visual C++ 2008 SP1
[0] None
Compiler: 6
The default location for Microsoft Visual C++ 2008 SP1 compilers is C:/Program Files/Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:/Program Files/Microsoft Visual Studio 9.0 anyway [y]/n? n
Please enter the location of your compiler: [C:/Program Files/Microsoft Visual Studio 9.0] D:/Program Files/Microsoft Visual Studio 9.0
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: D:/Program Files/Microsoft Visual Studio 9.0
Are these correct [y]/n? y
*********************************************************
Warning: Applications/components generated using Microsoft Visual Studio
2008 require that the Microsoft Visual Studio 2008 run-time
libraries be available on the computer used for deployment.
To redistribute your applications/components, be sure that the
deployment machine has these run-time libraries.
*********************************************************
Trying to update options file: C:/Users/Administrator/AppData/Roaming/MathWorks/MATLAB/R2009a/compopts.bat
From template: D:/PROGRA~1/MATLAB/R2009a/bin/win32/mbuildopts/msvc90compp.bat
Done . . .
注意低级的Matlab版本可能不支持VS2008编译环境。
参考文档:
1. http://blog.csdn.net/infocarrier/archive/2010/09/01/5854522.aspx
2. http://www.cnitblog.com/edaiqingwa/archive/2006/12/18/20741.aspx
3. http://blog.csdn.net/infocarrier/archive/2010/09/01/5854522.aspx
[A1]具体的函数名请见myadd.h文件