转自:http://blog.csdn.net/pipiqiqi123/article/details/7709683
本人刚接触VC,平时经常遇到matlab,看了几篇博文之后,写下自己的实现过程。
实验环境:
Win7
MATLAB 2010b(安装路径:E:\Program Files\MATLAB\R2010b)
VS2010 中文版(安装路径:C:\Program Files\Microsoft Visual Studio 10.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? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in E:\PROGRA~1\MATLAB\R2010b\sys\lcc
[2] Microsoft Visual C++ 2010 in C:\Program Files\Microsoft Visual Studio 10.0
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: C:\Program Files\Microsoft Visual Studio 10.0
Are these correct [y]/n? y
****************************************************************************
Warning: Applications/components generated using Microsoft Visual C++
2010 require that the Microsoft Visual Studio 2010 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\BaoQi\AppData\Roaming\MathWorks\MATLAB\R2010b\compopts.bat
From template: E:\PROGRA~1\MATLAB\R2010b\bin\win32\mbuildopts\msvc100compp.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, char* 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;
}
/
运行结果如下图: