VC和MATLAB混合开发经验总结

作者:朱金灿

来源:http://blog.csdn.net/clever101

 

前期准备:

       1.请确认机器中已经安装Matlab主程序或(MCR)MATLAB Compiler Runtime(具体到matlab安装目录下搜索MCRInstaller.exe)。

      

       2.准备好MATLAB编译好的C/C++文件:包括<my_dll>.h<my_dll>.lib <my_dll>.dll

         其中<my_dll>为生成的文件名。

        

建立工程:

       1.新建一个工程。将<my_dll>.h、<my_dll>.lib拷贝到工程文件夹下

         <my_dll>.dll拷贝到输出目录。

        

       2.将<my_dll>.h添加到工程中。

      

       3.在工程属性页 -> C/C++ -> 附加的包含目录 选项中添加包含目录

         <you_dir>R2009aexterninclude

         注:<you_dir>为计算机中matlab的安装位置。

                若以添加系统环境变量则可跳过这一步。

                若没有安装matlab,只安装MCR,则到MCR目录下找externinclude 也可。

               

       4.在工程属性页 -> 链接器 -> 附加库目录 选项中添加包含目录

         <you_dir>R2009aexternlibwin32microsoft

         注:<you_dir>为计算机中matlab的安装位置。

                若没有安装matlab,只安装MCR,则到MCR目录下找externlibwin32microsoft 也可。

               

       5.在工程属性页 -> 链接器 -> 输入 -> 附加依赖项 中添加依赖项

         mclmcrrt.lib mclmcr.lib dlltest.lib

        

调用dll:

       1.先执行mclInitializeApplication(NULL,0)以初始化MCR库,请检测返回值以确认初始化成功。

      

       2.执行<my_dll>.h 中的 <my_dll>Initialize(void)以初始化DLL,请检测返回值以确认初始化成功。

         注:初始化可以放到构造函数中运行。

        

       3.执行函数操作如mlfDraw_test(int nargout,mxArray** y)。

         注:若有图形界面函数为异步调用,既显示图像后即返回值。

                可以用mclWaitForFiguresToDie(NULL)来阻塞线程等待Matlab窗口关闭。

                *调用前请确认MCR及dll初始化成功,若不成功调用函数可导致程序崩溃。

      

       4.执行完函数后调用<my_dll>Terminate(void)来结束DLL。

      

       5.最后调用mclTerminateApplication()来结束MCR库。

         注:执行4函数时请确认所有Matlab已经关闭。

                如在有Matlab窗口时执行4函数可能发生未知错误!

             有如下两种方法确认:

             1.调用mclWaitForFiguresToDie(NULL)来阻塞线程等待Matlab窗口关闭。

                2.调用mclKillAllFigures(NULL)来强制关闭所有Matlab窗口。

        *注:上述4、5两个函数请勿放置在析构函数中是执行,若这样做有可能发生未知错误!

                请在宿主对象析构函数之前调用上述4、5函数!

               

3.部署调用matlab的程序时在安装MCR后,需要在Path环境变量设置MCR的路径,具体是setPATH=<mcr_root>v710 untimewin32;%PATH%             

 

               

第二部分Matlab编译C/C++dll:

 

Matlab 生成DLL

编译器的安装

       在matlab中先安装编译器,我在第一次安装的时候一路y下来,      

       只有一个compiler,还是最老的。这教育我们要学会说N,按照以下步骤操作

 

       >>mbuild -setup

 

       Pleasechoose your compiler for building standalone MATLAB applications:


       Wouldyou like mbuild to locate installed compilers [y]/n? n

 

       Selecta compiler:

 

       [1]Lcc-win32 C 2.4.1

 

       [2]Microsoft Visual C++ 6.0

 

       [3]Microsoft Visual C++ .NET 2003

 

       [4]Microsoft Visual C++ 2005 SP1

 

       [5]Microsoft Visual C++ 2008 Express

 

       [6]Microsoft Visual C++ 2008 SP1

 

       [0]None

 

       Compiler:6

 

       Thedefault location for Microsoft Visual C++ 2008 SP1 compilers is C:/ProgramFiles/Microsoft Visual Studio 9.0,

 

       butthat directory does not exist on this machine.

 

       UseC:/Program Files/Microsoft Visual Studio 9.0 anyway [y]/n? n

 

       Pleaseenter the location of your compiler: [C:/Program Files/Microsoft Visual Studio9.0] <you_dir_VS>/Microsoft Visual Studio 9.0

 

       Pleaseverify your choices:

 

       Compiler:Microsoft Visual C++ 2008 SP1

 

       Location:e:/Program Files/Microsoft Visual Studio 9.0

 

       Arethese correct [y]/n? y

 


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

 

         Warning: Applications/components generatedusing Microsoft Visual Studio 

 

                        2008 require that the Microsoft VisualStudio 2008 run-time     

 

                        libraries be available on the computer usedfor deployment.     

 

                        To redistribute your applications/components,be sure that the  

 

                        deployment machine has these run-timelibraries.                

 

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


       Tryingto update options file: C:/Users/Administrator/AppData/Roaming/MathWorks/MATLAB/R2009a/compopts.bat

 

       Fromtemplate:             E:/PROGRA~1/MATLAB/R2009a/bin/win32/mbuildopts/msvc90compp.bat

 

       Done. . .

 

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

       在编译生成C++库后同时生成一个readme文件,这个文件就是告诉你以后部署软件时该安装哪些库的。

原文地址:https://www.cnblogs.com/lanzhi/p/6470240.html