Matlab编译器使用mbuild命令可以直接将C/C++源代码编译为独立运行程序,在这些源代码中可以使用Matlab提供的接口来调用Matlab数学库函数。
虽然生成的可执行程序可以脱离Matlab环境运行,但是它需要Matlab C/C++数学库文件或者图形库文件的支持才能运行。但如果C/C++源代码完全是按传统C/C++源代码语法规则编写,没有包含Matlab数学库和图形库的调用,则不仅可以独立与Matlab环境,也不需要那两个库函数的支持。
1. MATLAB 环境配置:
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,要注意安装路径是否相符)
[1] Microsoft Software Development Kit (SDK) 7.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ 2008 SP1
[4] Microsoft Visual C++ 2010
[5] Microsoft Visual C++ 2012
[0] None
Compiler: 4
E:Program FilesVS2010. Do you want to use this compiler [y]/n? y
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: E:Program FilesVS2010
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:UsersYanluAppDataRoamingMathWorksMATLABR2013acompopts.bat
From template: E:PROGRA~1MATLAB~1inwin32mbuildoptsmsvc100compp.bat
Done . . .
>> mex – setup
以类似的方法配置编译器。Matlab 的配置完成.
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~1MATLABR2011bsyslcc
[2] Microsoft Visual C++ 2010 in D:Program FilesMicrosoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in E:Program FilesMicrosoft Visual Studio 9.0
[0] None
Compiler: 2
Compiler: Microsoft Visual C++ 2010
Location: E:Program FilesVS2010
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:UsersYanluAppDataRoamingMathWorksMATLABR2013amexopts.bat
From template: E:PROGRA~1MATLAB~1inwin32mexoptsmsvc100opts.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/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
Building with the -largeArrayDims option enables the new API.
**************************************************************************
2.转化BoostImage.m文件
mcc -W cpplib:BoostImage -T link:lib BoostImage -d F:zhangyingxieyulinMatlabToC
注:在此的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是一个类,具体可以查帮助。
2.系统环境设置
我的电脑->属性->高级->环境变量->用户变量->PATH中添加路径:D:Program FilesMATLABR2011binwin32,重启生效。
3.VS2010环境配置
新建一个win32 控制台应用程序,命名为MatlabTest.
右键MatlabTest解决方案->properties->VC++ Derections
(1)在“Executable Directions”项目栏,添加:D:Program FilesMATLABR2011binwin32(注意:当前的Mathlab安装在D:Program Files下面)
(2)在“include Directions”项目栏,添加:D:Program FilesMATLABR2011bexterninclude
(3)在“Library Directions”项目栏,添加:D:Program FilesMATLABR2011bexternlibwin32microsoft
上述环境只需要配置一次。
另参考:
在此暂时先测试了VS2010调用matlab现有函数的功能:
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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("