C++与Matlab混合编程
2015-03-11 20:54
330 查看
这个教程主要是写给自己看的,怕以后自己会忘记。
由Matlab的编写的函数.m文件生成.dll文件,.lib文件和.h文件供VC使用。
VS 2010
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,这三个文件将是我们需要的。
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++测试代码
输出结果为:
由Matlab的编写的函数.m文件生成.dll文件,.lib文件和.h文件供VC使用。
环境:
Matlab R2012bVS 2010
1.编辑Matlab的函数(.m文件),这里实现两个数组的加法(myADD.m)。
function c=myADD(a,b) c=a+b; end
2. Matlab编译.m文件生成动态链接库等
1)输入mex -setupWelcome 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相关文章推荐
- matlab与c++混合编程
- matcom进行matlab与c++混合编程
- Compiler_C++(Matlab_C++混合编程)
- Matlab与C++混合编程(依赖OpenCV)
- matlab和C++混合编程
- C++与matlab混合编程基于主成份分析算法的数值分析(一)
- C/C++与Matlab混合编程初探
- C/C++与Matlab engine 混合编程
- <zz>matlab程序与C/C++代码混合编程
- Matlab与C++混合编程
- Matlab与c++混合编程[三.生成可以独立运行的Matlab程序]
- Matlab和C++混合编程
- C/C++与Matlab engine 混合编程
- C++和MATLAB混合编程-DLL篇[转]
- 【Matlab】C++和MATLAB混合编程-前篇
- 谈谈Matlab与C/C++或C#的互调用(混合编程)
- Matlab与c++混合编程[一.几篇文章]
- linux系统中的C++/MATLAB混合编程
- Matlab与C++混合编程初探
- c++与matlab混合编程,vs2010中调用.mat文件