您的位置:首页 > 编程语言 > MATLAB

C++与Matlab混合编程

2015-03-11 20:54 330 查看
这个教程主要是写给自己看的,怕以后自己会忘记。

由Matlab的编写的函数.m文件生成.dll文件,.lib文件和.h文件供VC使用。

环境:

Matlab R2012b

VS 2010

1.编辑Matlab的函数(.m文件),这里实现两个数组的加法(myADD.m)。

function c=myADD(a,b)

c=a+b;

end


2. Matlab编译.m文件生成动态链接库等

1)输入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/R2012b/win64.html



Please choose your compiler for building MEX-files:



Would you like mex to locate installed compilers [y]/n? y



Select a compiler:

[1] Microsoft Visual C++ 2010 in d:\Program Files (x86)\Microsoft Visual Studio 10.0



[0] None



Compiler: 1



Please verify your choices:



Compiler: Microsoft Visual C++ 2010

Location: d:\Program Files (x86)\Microsoft Visual Studio 10.0



Are these correct [y]/n? y

***************************************************************************

Warning: MEX-files generated using Microsoft Visual C++ 2010 require

that Microsoft Visual Studio 2010 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\Administrator\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat

From template: D:\PROGRA~1\MATLAB\R2012b\bin\win64\mexopts\msvc100opts.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/help/techdoc/matlab_external/bsflnue-1.html

Building with the -largeArrayDims option enables the new API.

**************************************************************************

选择VS2010 编译器

2)编译

输入mcc -W cpplib:myADD -T link:lib myADD.m

在同一个文件夹下将生成myADD.dll,myADD.lib和myADD.h,这三个文件将是我们需要的。

3.VS2010项目下调用Matlab的myADD函数

新建VS2010空项目(64位的项目工程),将myADD.dll放入Matlab安装文件夹的bin文件夹下(D:\Program Files\MATLAB\R2012b\bin),将myADD.lib放入库文件夹下(这里设为F:\VS Project\matlab2c\lib),将myADD.h放入VS工程文件夹下。

VS的工程属性设置,包含文件(其中D:\Program Files\MATLAB\R2012b是Matlab的安装位置)



库目录设置:



链接器-》输入-》附加依赖项



此外检查系统环境变量Path是否包含下列位置,如果没有,则手动添加进去

D:\Program Files\MATLAB\R2012b\bin;

D:\Program Files\MATLAB\R2012b\bin\win64;

D:\Program Files\MATLAB\R2012b\runtime\win64;

编辑c++测试代码

#include "myADD.h"
#include <iostream>
using namespace std;

int main(){
	cout<<"Initialize"<<endl;
	 if(!myADDInitialize())  
     {  
       cout<<"初始化失败!"<<endl;  
       return -1;  
     }  

	 cout<<"Initialize Done!"<<endl;
	mwArray a(1,2,mxDOUBLE_CLASS);
	mwArray b(1,2,mxDOUBLE_CLASS);
	mwArray c(1,2,mxDOUBLE_CLASS);
	a(1,1)=1;
	a(1,2)=2;
	b(1,1)=7;
	b(1,2)=4;
	//第一个参数为输出参数的个数,c为输出,a和b为输入
	myADD(1,c,a,b);

	double *C;
	C=new double[2];
	c.GetData(C,2);
	for(int i=0;i<2;i++)
		cout<<C[i]<<" ";
	cout<<endl;
		 
	delete [] C;
	while(1);
	return 0;
}


输出结果为:



参考:

[1]. http://blog.csdn.net/ithzhang/article/details/7444563 [2]. http://blog.csdn.net/ithzhang/article/details/7441092 [3]. http://blog.csdn.net/blackhuman01/article/details/6312798 [4]. http://jingyan.baidu.com/article/8ebacdf030594c49f65cd5cd.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: