Visual Studio2010与Matlab
R2012b 交叉编程 配置步骤 BY Canny0222
配置环境:
系统Win 8,64位。
MATLAB R2012b(安装路径:C:\Program
Files\MATLAB\R2012a)
VS2010(安装路径:C:\Program
Files (x86)\Microsoft Visual Studio 10.0)
目的:采用由m文件构造动态链接库生成cpp文件,然后在visual
studio中调用。
1.
MATLAB 环境配置:
>> mbuild -setup
Welcome to mbuild -setup. This utility
will help you set up
a default compiler. For a list of
supported compilers, see
http://www.mathworks.com/support/compilers/R2011b/win32.html
Please choose your compiler for building standalone MATLAB
applications:
Would you like mbuild to locate installed compilers [y]/n?
y
(注:若是第一次mbuild -setup,选n,再在给出的的选项中选择2010
compiler,要注意安装路径是否相符)
Select a compiler:
[1] Lcc-win32 C 2.4.1 in
D:\PROGRA~1\MATLAB\R2011b\sys\lcc
[2] Microsoft Visual C++ 2010 in
D:\Program Files\Microsoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in E:\Program
Files\Microsoft Visual Studio 9.0
[0] None
Compiler: 2 %选择Microsoft
Visual C++ 2010 的号码
Please verify your choices:
Compiler: Microsoft Visual C++
2010
Location: D:\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:\Documents and
Settings\jiao\Application
Data\MathWorks\MATLAB\R2011b\compopts.bat
From
template: D:\PROGRA~1\MATLAB\R2011b\bin\win32\mbuildopts\msvc100compp.bat
Done . . .
>> 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/R2011b/win32.html
Please choose your compiler for building
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\R2011b\sys\lcc
[2] Microsoft Visual C++ 2010 in
D:\Program Files\Microsoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in E:\Program
Files\Microsoft Visual Studio 9.0
[0] None
Compiler: 2
%同上,选择
Please verify your choices:
Compiler: Microsoft Visual C++
2010
Location: D:\Program Files\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:\Documents and
Settings\jiao\Application
Data\MathWorks\MATLAB\R2011b\mexopts.bat
From
template: D:\PROGRA~1\MATLAB\R2011b\bin\win32\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/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new
API.
**************************************************************************
2.转化BoostImage.m文件
在matlab中
>>mcc -W cpplib:BoostImage -T
link:lib BoostImage -d F:\zhangying\xieyulin\MatlabToC\
注:在此的m文件function最好对应end,否则容易出现
Error using mcc的错误。(但我碰到有个m文件仍不能转化成功,不知什么原因)
其中,mcc是Matlab提供的编译命令,对于这一点,Matlab的帮助说明如下:
-W lib:string link:lib其中-W是控制编译之后的封装格式,cpplib,是指编译成C++的lib,cpplib冒号后面是指编译的库的名字,
-T表示目标,link:lib表示要连接到一个库文件的目标,目标的名字是后面的BoostImage,即你写的m函数的名字。
运行完之后,你的目录下面,将会出现以下一些文件:
BoostImage.cpp
BoostImage.exp
BoostImage.dll
BoostImage.exports
BoostImage.h
BoostImage.lib
mccExcludedFiles.log
readme.txt
具体每一个文件的意思,请查帮助,这里只说其中很有用的几个:
BoostImage.dll是编译好的动态库,BoostImage这个函数封装在里面,BoostImage.h是对应的头文件,在C里面调用这个函数的时候,需要include这个头文件在这个头文件的最后,你可以看到下面的内容:
extern LIB_BoostImage_CPP_API void MW_CALL_CONV
BoostImage(int nargout, mwArray& output_im, const mwArray&
input_im, const mwArray& Mboost);
这就是BoostImage的C函数的声明。nargout表示输出变量的个数,a,b,c就是对应了m函数的几个变量。
注意,a,b,c三个变量的类型是mwArray,这是Matlab提供的一个特殊的变量类型(在7.0的版本,编译之后,变量类型是mxArray)。mwArray是一个类,具体可以查帮助。
4.系统环境配置
我的电脑->属性->高级->环境变量->用户变量->PATH中添加路径:C:\Program
Files\MATLAB\R2012a\bin\win64,重启生效。
5、VS2010环境配置
新建一个win32 控制台应用程序,命名为MatlabTest.
右键MatlabTest解决方案->属性->VC++
目录
Win8 64位的情况:
(1)在“可执行文件目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\bin\win64(注意:当前的Mathlab安装在C:\Program
Files下面)
(2)在“包含目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\extern\include
(3)在“库目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\extern\lib\win64\microsoft
上述环境只需要配置一次。
注意:在64位操作系统上依然可以找到MATLAB相应文件夹下都有win32
和win64子文件夹,但是在32位操作系统上没有win64子文件夹。
Win7、8 32位的情况:
(1)在“可执行文件目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\bin\win32
(2)在“包含目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\extern\include
(3)在“库目录”项目栏,添加:C:\Program
Files\MATLAB\R2012a\extern\lib\win32\microsoft
右键MatlabTest解决方案->属性->链接器->输入
在“附加依赖项中”中添加相应的静态链接库文件。对于需要添加的静态库文件的数量和名称,根据需要添加。
libmx.lib
libeng.lib
libmex.lib
libmat.lib
…………
根据需要后续补上。
在此暂时先测试了VS2010调用matlab现有函数的功能:
代码如下:
///
// MatlabTest.cpp : Defines the entry
point for the console application.
//
//#include "stdafx.h"
#include
#include
#include
#include
"engine.h"
#include
"matrix.h"
#pragma
comment(lib,"libeng.lib")
#pragma
comment(lib,"libmx.lib")
int main()
{
Engine *ep;
int i , j ;
//show how to open MATLAB
engine
//for remote ones:
//engOpen( ADDRESS OF REMOTE SYSTEM )
;
if (!(ep = engOpen("\0"))){
fprintf(stderr, "\nCan't start MATLAB
engine\n");
return EXIT_FAILURE;
}
//show how to create
matrix
mxArray *Y = mxCreateDoubleMatrix(1 , 3 , mxREAL)
;
//show how to put data in
matrix
double tmp[3] = {1.0 , 2.0 , 3.0} ;
memcpy(mxGetPr(Y) , tmp , sizeof(tmp))
;
//show how to put variables in the
Engine
engPutVariable(ep , "Y" , Y) ;
//show how to execute commands in
MATLAB
engEvalString(ep, "X = ones(5,1) *
Y");
//show how to get variables from the
Engine
mxArray *X = engGetVariable(ep , "X")
;
//show how to manipulate
dimensions
int dims[10] ;
int ndims ;
ndims = mxGetNumberOfDimensions(X) ;
printf("total number of dimensions is %d\n" , ndims)
;
memcpy(dims , mxGetDimensions(X) , ndims * sizeof(int))
;
for ( i = 0 ; i < ndims ; i ++ ){
printf("dimension %d : %d\n" , i , dims[i])
;
}
printf("\n") ;
//show how the data is stored in the
memory
double *p = (double*)mxGetData(X)
;
for ( i = 0 ; i < dims[0] ; i ++ ){
for ( j = 0 ; j < dims[1] ; j ++ ){
printf("%8.2f" , p[j * dims[0] + i]) ;
}
printf("\n") ;
}
//---important, to release
resources
mxDestroyArray(X) ;
mxDestroyArray(Y) ;
//show how to hide and unhide MATLAB command
window
printf("type RETURN to hide the MATLAB command window...\n")
;
getchar() ;
engSetVisible(ep , false) ;
printf("type RETURN to unhide the MATLAB command window...\n")
;
getchar() ;
engSetVisible(ep , true) ;
printf("type RETURN to END this program...\n")
;
getchar()
;
//remembering to close it is important
.
//but if you are debugging your programs
,
//annotate the following line will save you a lot of time
,
//for you needn't to restart the Engine
.
engClose(ep) ;
//when your work is accomplished , type "exit" in MATLAB command
window
return EXIT_SUCCESS;
}
///
结果: