转载:
刚开始学习用VC++调用matlab生成的DLL,找了网上一些资料,难以找到vs2008与MATLAB2009b版本的,按照以往版本做的总是有很多错误。经过两天努力,终于调试成功,这里将经验总结一下,以供有需要的人们参考。
实验环境:
Win7
MATLAB 2009b(安装路径:E:/Program Files/MATLAB/R2009a)
VS2008 中文版(安装路径:E:/Program Files/Microsoft Visual Studio 9.0)
1.Matlab 生成DLL
1.1编译器的安装
在matlab中先安装编译器,我在第一次安装的时候一路y下来,只有一个compiler,还是最老的。这教育我们要学会说N,按照以下步骤操作
>> 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] e:/Program Files/Microsoft Visual Studio 9.0
(红色部分换成你的vs所安装的地址)
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: e:/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: E:/PROGRA~1/MATLAB/R2009a/bin/win32/mbuildopts/msvc90compp.bat
Done . . .
1.2 DLL的生成
首先新建一个m文件,文件名为myadd2.m,定义了一个名为myadd2的函数,代码如下:
//////////////////////////////////////////////////////
function [y,z] = myadd2(a, b)
% dummy function, just to
demonstrate the idea
y = a+b;
z = a+2*b;
end
/////////////////////////////////////
在MATLAB命令框中输入以下命令:
mcc -W cpplib:libmyadd2 -T link:lib myadd2.m
生成libmyadd2.lib, libmyadd2.h, libmyadd2.dll
等文件,将这三个文件拷到VS的项目目录下
2. VS调用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 编写主程序,调试运行
这段代码是从别处拷来的,是一段完整代码,将它粘到matlabDLL2.cpp 主CPP文件中,调试通过:
//////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
#include
"mclmcr.h"
#include "mclcppclass.h"
#include "libmyadd2.h"
int
_tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello
world!" << std::endl;
/* Initialize the MCR */
/* if( !mclInitializeApplication(NULL,0) )
{
std::cout << "Could not initialize the application!"
<< std::endl;
return -1;
}
*/
// initialize lib
if(
!libmyadd2Initialize())
{
std::cout <<
"Could not initialize libmyadd2!" << std::endl;
return
-1;
}
try
{
// declare and
initialize a
mwArray a(2, 2, mxDOUBLE_CLASS);
double
*aData;
aData = new double[4];
int i;
for( i=0;
i<4; ++i)
{
aData[i] = 1.0*i;
}
// print output
std::cout << "a = " <<
std::endl;
std::cout << aData[0] << ",/t" <<
aData[1] << std::endl;
std::cout << aData[2] <<
",/t" << aData[3] << std::endl;
a.SetData(aData,
4);
// declare and initialize b
mwArray b(2,
2, mxDOUBLE_CLASS);
b(1,1) = 11.;
b(1,2) = 12.;
b(2,1) = 21.;
b(2,2) = 22.;
mwArray y(2,
2, mxDOUBLE_CLASS);
mwArray z(2, 2, mxDOUBLE_CLASS);
// call the function
myadd2(2, y, z, a, b);
// copy data from mwArray to C++ objects
// allocate
outputs
double *yData, *zData;
yData = new double[4];
if( yData == NULL )
{
std::cout <<
"Failed to allocate memory for yData!" << std::endl;
return -1;
}
zData = new double[4];
if(
zData == NULL )
{
std::cout << "Failed to
allocate memory for zData!" << std::endl;
return
-1;
}
// copy data from mwArray to C++
y.GetData(yData, 4);
z.GetData(zData, 4);
// print
output
std::cout << "y = " << std::endl;
std::cout << yData[0] << ",/t" << yData[1] <<
std::endl;
std::cout << yData[2] << ",/t" <<
yData[3] << std::endl;
std::cout << "z = " <<
std::endl;
std::cout << zData[0] << ",/t" <<
zData[1] << std::endl;
std::cout << zData[2] <<
",/t" << zData[3] << std::endl;
//
deallocate memory
delete [] aData;
delete [] zData;
delete [] yData;
}
catch( const mwException&
e)
{
std::cerr << e.what() <<
std::endl;
}
// terminate the
lib
libmyadd2Terminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
/////////////////////////////////////////////////////////////////////////////
运行结果如下图: