转载:http://blog.youkuaiyun.com/infocarrier/article/details/5854522,但是还是有些修改
刚开始学习用VC++调用matlab生成的DLL,找了网上一些资料,难以找到vs2008与MATLAB20010a版本的,按照以往版本做的总是有很多错误。经过两天努力,终于调试成功,这里将经验总结一下,以供有需要的人们参考。
实验环境:
Win7
MATLAB 2010a(安装路径:E:/Program Files/MATLAB/R2009a)
VS2008 中文版(安装路径:E:/Program Files/Microsoft Visual Studio 9.0)
1.Matlab 生成DLL
1.1配置matlab
在matlab中先安装编译器,我在第一次安装的时候一路y下来,只有一个compiler,还是最老的。这教育我们要学会说N,按照以下步骤操作
>> mex -setupPlease 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\R2010a\sys\lcc
[2] Microsoft Visual C++ 2010 in D:\Program Files\Microsoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in D:\Program Files\Microsoft Visual Studio 9.0
[4] Microsoft Visual C++ 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None
Compiler: 3
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: MEX-files generated using Microsoft Visual C++ 2008 require
that Microsoft Visual Studio 2008 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\Hellen\AppData\Roaming\MathWorks\MATLAB\R2010a\mexopts.bat
From template: D:\PROGRA~1\MATLAB\R2010a\bin\win32\mexopts\msvc90opts.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/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
**************************************************************************
>> 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-win32 C 2.4.1 in D:\PROGRA~1\MATLAB\R2010a\sys\lcc
[2] Microsoft Visual C++ 2008 SP1 in D:\Program Files\Microsoft Visual Studio 9.0
[3] Microsoft Visual C++ 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None
Compiler: 2
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\Hellen\AppData\Roaming\MathWorks\MATLAB\R2010a\compopts.bat
From template: D:\PROGRA~1\MATLAB\R2010a\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 控制台应用程序,我取的名字是matlabDll.当然新建其他的项目类型也可以,我这只是个例子。接下来进行配置,在该项目的属性中进行了配置,只对该项目有效。若建新的项目需要重新配置。项目建好后将libmyadd2.lib, libmyadd2.h, libmyadd2.dll拷贝到项目目录下。
首先配置项目属性页/配置属性/C-C++/常规/附加包含目录,请根据自己电脑上软件的安装位置对照设置,2008与2005不同的地方时这里要加两个目录,如下图所示:
其次配置项目属性页/配置属性/链接器/常规/附加库目录,请根据自己电脑上软件的安装位置对照设置,如下图所示:
然后配置项目属性页/配置属性/链接器/输入/附加依赖性,填入libmyadd2.lib mclmcrrt.lib mclmcr.lib ,如下图所示:
在这一步俺可吃了大苦头了,有篇文档只说添加前两项,俺就照做了,结果导致运行失败,找了好长时间也没发现错误,两天的功夫都在找,结果就是因为少填了这一项,还有其他的一些包含lib,比如libmex.lib libmx.lib ,这里没填,好像是VC6上要配的。
配置到此结束
注意,其实mclmcrrt.lib也是没用要!!
2.2 编写主程序,调试运行
这段代码是从别处拷来的,是一段完整代码,将它粘到matlabDLL2.cpp 主CPP文件中,调试通过:
#include <iostream>
//#include "mclmcr.h"
//#include "mclcppclass.h"
#include "libmyadd2.h"
#pragma comment(lib, "libmyadd2.lib")
#pragma comment(lib, "mclmcrrt.lib")
int main()
{
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;
}
运行结果如下图:
问题
我将代码的标红部分注释掉了,否则会出现错误:
1>c:/users/administrator/documents/visual studio 2008/projects/matlabdll2/matlabdll2/matlabdll2.cpp(14) : error C3861: “mclInitializeApplication_proxy”: 找不到标识符
我不知道为什么,大家在调试的过程如果解决了这个问题,麻烦告诉一声:liufengjk_ok@126.com。
致谢
感谢taohe等网友提供的参考!