Matlab 2013a 和 VS2010 混合编程

最近由于项目需求,某项目的算法是基于MATLAB完成的,在短时间内需要去调用算法功能。因此,基于MATLAB生成DLL, C 调用的方式完成。

环境:MATLAB 2013a + VS2010 + win8.1 + 64位系统

一、MATLAB 编译环境设置

1. 安装,MATLAB安装时选择 force 32bit, 这样生成的dll在32位和64位的机器上都可以调用。

2. 装好MATLAB 2013后,打开软件,进行配置

 >> mex -setup
Welcome to mex -setup. This utility will help you set up
a default compiler. For a list of supported compilers, see
http://www.mathworks.com/support/compilers/R2013a/win32.html
Please choose your compiler for building MEX-files:
Would you like mex to locate installed compilers [y]/n?y
Select a compiler:
[] Lcc-win32 C 2.4. in D:\PROGRA~\MATLAB\R2013a\sys\lcc
[] Microsoft Visual C++ in D:\Program Files (x86)\Microsoft Visual Studio 10.0 [] None Compiler: Please verify your choices: Compiler: Microsoft Visual C++
Location: D:\Program Files (x86)\Microsoft Visual Studio 10.0 Are these correct [y]/n? y ***************************************************************************
Warning: MEX-files generated using Microsoft Visual C++ require
that Microsoft Visual Studio run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.
*************************************************************************** Trying to update options file: C:\Users\robin\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
From template: D:\PROGRA~\MATLAB\R2013a\bin\win32\mexopts\msvc100opts.bat Done . . .

显示 Done 后执行下一步

 >> mbuild -setup

 Welcome to mbuild -setup.  This utility will help you set up
a default compiler. For a list of supported compilers, see
http://www.mathworks.com/support/compilers/R2013a/win32.html Please choose your compiler for building shared libraries or COM components: Would you like mbuild to locate installed compilers [y]/n? y Select a compiler:
[] Microsoft Visual C++ in D:\Program Files (x86)\Microsoft Visual Studio 10.0 [] None Compiler: Please verify your choices: Compiler: Microsoft Visual C++
Location: D:\Program Files (x86)\Microsoft Visual Studio 10.0 Are these correct [y]/n? y ****************************************************************************
Warning: Applications/components generated using Microsoft Visual C++
require that the Microsoft Visual Studio 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\robin\AppData\Roaming\MathWorks\MATLAB\R2013a\compopts.bat
From template: D:\PROGRA~\MATLAB\R2013a\bin\win32\mbuildopts\msvc100compp.bat Done . . .

这样,MATLAB的Mex编译环境就配置好了。

二、MATLAB 代码编译dll

打开MATLAB源代码,找到最顶层的m文件,注意顶层必须为function方式。

例如,MATLAB的顶层函数为

文件 ship_track1.m

 function finish_flag = ship_track1(input_x,input_y,num_ori)

为某目标检测算法,input_x ,input_y,和num_ori为输入的3个参数

那么编译的指令为

 >> mcc -W cpplib:libship_track1 -T link:lib ship_track1.m -C

配置完成后会在MATLAB工程目录生成7个文件

 libship_track1.cpp
libship_track1.ctf
libship_track1.dll
libship_track1.exp
libship_track1.exports
libship_track1.h
libship_track1.lib

当然我们需要用到的只有4个

 libship_track1.ctf
libship_track1.dll
libship_track1.h
libship_track1.lib

三、VS2010 MFC 工程配置

1. 新建VS2010 MFC 工程

2. 将第二步MATLAB编译生成的4个文件拷贝入MFC工程目录下

将头文件 libship_track1.h 添加进工程

3. 项目属性设置

1)选择项目->项目属性->所有配置

Matlab 2013a 和 VS2010 混合编程

C/C++ -> 常规->附加包含目录,找到MATLAB下的include目录

 D:\Program Files (x86)\MATLAB\R2013a\extern\include

(根据MATLAB的安装路径确定)

2)链接器->常规->附加库目录

 D:\Program Files (x86)\MATLAB\R2013a\extern\lib\win32\microsoft

Matlab 2013a 和 VS2010 混合编程

3) 链接器->输入->附加依赖项

 libship_track1.lib
libship_track2.lib
mclmcrrt.lib
libmx.lib
libmat.lib
mclmcr.lib

Matlab 2013a 和 VS2010 混合编程

到此就配置完成。

四、MFC 调用DLL

1. 初始化

 if(!libship_track1Initialize())    //初始化1通道
{
//MessageBox(_T("could not initialize the application"));
}

2. 传递参数

 double _Data1_1[] = {};                //输入数组
double _Data1_2[] = {};
for(int i=; i< chn1_num; i++)
{
_Data1_1[i] = Chan1_x[i];
_Data1_2[i] = Chan1_y[i];
} mxArray* Data1_1 = mxCreateDoubleMatrix(, ,mxREAL); //创建1*5矩阵
memcpy(mxGetPr(Data1_1), (void*)_Data1_1,sizeof(_Data1_1)); //拷贝输入数据
mxArray* Data1_2 = mxCreateDoubleMatrix(, ,mxREAL); //创建1*5矩阵
memcpy(mxGetPr(Data1_2), (void*)_Data1_2,sizeof(_Data1_2)); //拷贝输入数据
double _Data1_3[];
_Data1_3[] = chn1_num;
mxArray* Data1_3 = mxCreateDoubleMatrix(, ,mxREAL); //创建1*1矩阵;
memcpy(mxGetPr(Data1_3), (void*)_Data1_3,sizeof(_Data1_3)); //拷贝输入数据
mxArray *input1[] = {Data1_1,Data1_2,Data1_3}; //将矩阵x的指针作为输入参数传递进去
//mxArray *output[1]; //定义输出参数指针
mlxShip_track1(,NULL, , input1);

mxArray是Matlab C 函数库的结构体,需要利用它来进行参数传递。

对于mlxShip_track1函数来说,其四个参数分别对应:

mlxShip_track1(输出参数个数、输出参数、输入参数个数、输入参数)

上一篇:matlab + c/c++ opencv 混合编程


下一篇:Partial Tree---hdu5534(完全背包)