本帖介绍将matalb7的m函数打包成dll库的方法,具体步骤如下:
1.编译器设置
因为打包的m函数dll库要被VC调用,因此,在打包过程中,需要将matlab7的编译器设置为VC所用的编译器。具体设置方法如下:
(1)在matlab7的Command Window中输入命令
mbuild -setup
matlab7将给出如下提示:
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n?
这时,可以输入y或n,下面分别介绍输入y和n的情况。
(2)如果输入y,matlab将给出可选编译器,供选择,提示如下:
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\PROGRA~1\MATLAB\R2010b\sys\lcc
[2] Microsoft Visual C++ 2008 SP1 in D:\Program Files\Microsoft Visual Studio 9.0
[3] Microsoft Visual C++ 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None
Compiler:
这时在Compiler: 后面输入1、2、3或0就可以设置编译器了,其中:1是matlab7默认的编译器,2是vc2008的编译器,3是VC6.0的编译器。
在我们的应用中,编译matlab函数的dll是为了VC程序调用,因为我们选择编译器2或3就可以了。
输入编译器对应的数字后,matlab给出如下提示:
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: D:\Program Files\Microsoft Visual Studio 9.0
Are these correct [y]/n?
确认无误后,输入y就可以了。
(3)如果输入n,matlab将给出比输入y时更多的可选编译器,供选择,提示如下:
Select a compiler:
[1] Lcc-win32 C 2.4.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ 2005 SP1
[4] Microsoft Visual C++ 2008 Express
[5] Microsoft Visual C++ 2008 SP1
[6] Microsoft Visual C++ 2010
[7] Microsoft Visual C++ 2010 Express
[0] None
Compiler:
与情况(2)相同通过输入编译器的编号来选择编译器就可以了。
在实际应用中,我们按上述(2)所述的方法操作就可以了。
2.打包*.dll库的方法
(1)打包成c库
mcc -W lib:你的库名称 -T link:lib m函数1的文件名 m函数2的文件名 m函数3的文件名
(2)打包成c++库
mcc -W cpplib:你的库名称 -T link:lib m函数1的文件名 m函数2的文件名 m函数3的文件名
需要注意:
a.库名称的开头最好是lib开头,这样不会产生警告(帮助里这么说的);
b.m函数的文件名中不包含matlab文件的后缀“.m”;
c.上述方法将把多个m函数打包在一个*.dll库中。
(4)库打包后所形成文件的存放位置
a.打包后形成的所有文件中,只有4个文件是我们在VC编程时需要用到的,它们的文件名称是1.中的“你的库名称”,文件后缀名分别是:*h,*.lib,*.dll,*.ctf
b.打包后的相关文件的存放位置(非常重要,需要注意)
(b.1)*h和*.lib应放在VC工程文件夹下,VC对程序进行编译连接,生成Debug和Release文件夹及其下的目标代码时使用.这两个文件放错位置,VC程序编译连接时会提示错误。
(b.2)*.dll,*.ctf应放在Debug和Release文件夹下,*.exe程序在执行使需要它们,否则即使程序能够编译连接形成*.exe,但是当程序需要用到matlab的DLL是还是会提示出错的,这个错误只有当实际运行你的程序时才会产生。
3.mcc 指令在matlab help中的MATLAB Compiler部分有讲解
4. 打包过程中可能出现的异常及处理方法
(1)打包过程中,产生了警告 No match builtin function available for set_param.bi
解决办法在这个网址http://www.ilovematlab.cn/viewthread.php?tid=19463里有介绍,下面将其中wetyboy给出方法写在下面,这个方法非常奏效:
解决办法:
To resolve this issue, you should modify the following file: $MATLAB/toolbox/compiler/deploy/matlabrc.m (where $MATLAB = the MATLAB root directory on your machine) Comment out the following lines (numbers 81 and 82) that have a call to SET_PARAM. set_param(0,'PaperType',defaultpaper); set_param(0,'PaperUnits',defaultunits); When you are finished, save these changes and issue the following command at the MATLAB command prompt: rehash toolboxcache ("Comment out "means to add a smybol "%"to the front of a sentance,) 英语的意思就是把这两行的命令给注释掉,然后在主窗口中,输入 rehash toolboxcache ,然后Enter 就可以了。我试过的! |
这样做以后,如果不把matlabrc.m再改回去会对其它matlab程序造成什么影响,我还没试过。
(2)出现错误提示
??? Error using ==> mbuild
Unable to complete successfully
??? Error: An error occurred while shelling out to mbuild (error code = 1).
Unable to build executable (specify the -v option for more information).
出现该错误的原因是,在用mcc -W cpplib指令打包库之前没有按照步骤1设置为vc的编译器,而采用matlab编译器在打包库时会产生这种错误。
5.参考资料
(1)matlab帮助
(2)http://blog.sina.com.cn/s/blog_49cb42490100nm5j.html
完毕!