1.安装编译器
>> mex -setup
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc C version 2.4 in C:\MATLAB7\sys\lcc
[0] None
Compiler: 1
Please verify your choices:
Compiler: Lcc C 2.4
Location: C:\MATLAB7\sys\lcc
Are these correct?([y]/n): y
Try to update options file: C:\Documents and Settings\liu\Application Data\MathWorks\MATLAB\R14\mexopts.bat
From template: C:\MATLAB7\BIN\WIN32\mexopts\lccopts.bat
Done . . .
安装完毕
说明:在命令窗口输入 mbuild –setup ,对于大多数用户来说选择Matlab自带的Lcc编译器是足够!如果要在没有安装matlab的计算机上执行编译后的程序,首先要将\MATLAB7\toolbox\compiler\deploy\win32中的MCRinstaller.exe安装到该计算机上(7.0以前的版本是mglinstaller.exe)。
2.设置编译器:
在确定安装好Matlab Compiler后,还需要对Compiler进行适当的配置,方法是在Matlab命令窗口输入:
>>mbuild –setup, 按提示选择matlab自带编译器LCC。
3.将脚本编译为可执行文件:
如项目文件包含:gui.m, gui.gif, fun1.m。
在此路径下命令行输入:>>mcc –m gui.m,生 成:mccExcludedFiles.log,ReadMe.txt,gui.ctf,gui.prj,gui_main.c,gui_mcc_component_data.c,gui.exe。其中:gui.ctf,gui.exe为脱离matlab环境运行必需的文件。
4.在未安装matlab的机器上运行可执行程序。
将toolbox\compiler\deploy\win32中的MCRinstaller.exe安装到该计算机上,将生成可执行程序脱离matlab运行所需的函数库。
将2中生成的gui.ctf,gui.exe先拷贝到该计算机同一路径。再运行gui.exe将生成gui_mcr文件夹,包含程序运行所需的库。至此完成。
5.去除独立可执行程序运行时的“DOS黑窗口”。
以上生成的exe程序运行时首先弹出一个DOS界面窗口,如果不需要其输出数据和错误信息,可将其去除。
matlba命令行输入:
>>cd(prefdir)
>>edit compopts.bat
此时compopts.bat打开,在文件最后添加:
set LINKFLAGS=%LINKFLAGS% -subsystem windows
如果程序比较复杂,易出现异常,则不建议去除这个dos窗口以便调试。
另外,这个办法是适用于你用的编译器为LCC,如果是其他的,那么所加语句有所不同
Microsoft Visual C/C++:
set LINKFLAGS=%LINKFLAGS% /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
Borland:
set LINKFLAGS=%LINKFLAGS% -aa
6.遇到某些函数不能使用的问题。
我的程序中用到vpa函数,运行gui.exe出现:undefined method or function 'vpa' for input argument type of 'double'错误提示。而gui.m脚本在matlab环境可以运行。
将matlab的GUI转换成可执行文件exe
11:02 2012-11-18
"C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R14"rem ********************************************************************
rem set MATLAB=%MATLAB%
set MATLAB=C:\MATLAB\R2008a
set PATH=%MATLAB%\sys\lcc\bin;%PATH%
set LCCMEX=%MATLAB%\sys\lcc\mex
set PERL="%MATLAB%\sys\perl\win32\bin\perl.exe"
===============================================
>> mcc -v -m 'SineWave.m'
Compiler version: 4.8 (R2008a)
Processing C:\MATLAB\R2008a\toolbox\matlab\mcc.enc
Processing d:\My Documents\MATLAB\sound\GUI\SineWave.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\guide\guideopts.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\winfun\actxcontrolselect.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\winfun\actxcontrolcreateproperty.fig
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
1 item(s) added.
Generating MATLAB path for the compiled application...
Created 39 path items.
Begin validation of MEX files: Sun Nov 18 10:53:09 2012
Validating 'C:\MATLAB\R2008a\toolbox\compiler\deploy\deploywhich.mexw32'.
No conflicting M-file found.
Validating 'C:\MATLAB\R2008a\toolbox\compiler\deploy\readline.mexw32'.
No conflicting M-file found.
End validation of MEX files: Sun Nov 18 10:53:09 2012
Deleting 2 temporary MEX authorization files.
Removing: 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\mathworks_tmp_5124_5880_5124.auth'.
Removing: 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\mathworks_tmp_5124_13944_5124.auth'.
Parsing file "d:\My Documents\MATLAB\sound\GUI\SineWave.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\MATLAB\R2008a\toolbox\compiler\deploy\deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\MATLAB\R2008a\toolbox\compiler\deploy\printdlg.m"
(Referenced from: "Compiler Command Line").
Generating file "SineWave_main.c".
Generating file "d:\My Documents\MATLAB\sound\GUI\readme.txt".
Generating file "SineWave_mcc_component_data.c".
Executing command: mbuild -O -v -output "SineWave" "SineWave_main.c" "SineWave_mcc_component_data.c" -link exe
This is mbuild Copyright 1984-2004 The MathWorks, Inc.
-> Default options filename found in C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R14
----------------------------------------------------------------
-> Options file = C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R14\compopts.bat
-> COMPILER = lcc
-> Compiler flags:
COMPFLAGS = -c -Zp8 -I"E:\MATLAB7\\sys\lcc\include" -noregistrylookup
OPTIMFLAGS = -DNDEBUG
DEBUGFLAGS = -g4
arguments =
Name switch = -Fo
-> Pre-linking commands =
-> LINKER = lcclnk
-> Link directives:
LINKFLAGS = -tmpdir "." -L"E:\MATLAB7\\sys\lcc\lib" -libpath "E:\MATLAB7\\extern\lib\win32\lcc"
LINKFLAGSPOST = mclmcrrt.lib
Name directive = -o "SineWave.exe"
File link directive =
Lib. link directive =
Rsp file indicator = @
-> Resource Compiler =
-> Resource Linker =
----------------------------------------------------------------
--> "lcc -c -Zp8 -I"E:\MATLAB7\\sys\lcc\include" -noregistrylookup -FoSineWave_main.obj -IE:\MATLAB7\\extern\include -IE:\MATLAB7\\simulink\include -DNDEBUG SineWave_main.c"
E:\MATLAB7\\BIN\WIN32\MEX.PL: Error: Compile of 'SineWave_main.c' failed.
Error: An error occurred while shelling out to mbuild (error code = 255).
Unable to build executable.
??? Error using ==> mcc
Error executing mcc, return status = 1.
>> mcc -v -m 'SineWave.m'
Compiler version: 4.8 (R2008a)
Processing C:\MATLAB\R2008a\toolbox\matlab\mcc.enc
Processing d:\My Documents\MATLAB\sound\GUI\SineWave.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\guide\guideopts.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\winfun\actxcontrolselect.fig
Processing C:\MATLAB\R2008a\toolbox\matlab\winfun\actxcontrolcreateproperty.fig
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
1 item(s) added.
Generating MATLAB path for the compiled application...
Created 39 path items.
Begin validation of MEX files: Sun Nov 18 11:01:16 2012
Validating 'C:\MATLAB\R2008a\toolbox\compiler\deploy\deploywhich.mexw32'.
No conflicting M-file found.
Validating 'C:\MATLAB\R2008a\toolbox\compiler\deploy\readline.mexw32'.
No conflicting M-file found.
End validation of MEX files: Sun Nov 18 11:01:16 2012
Deleting 2 temporary MEX authorization files.
Removing: 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\mathworks_tmp_1272_22690_1272.auth'.
Removing: 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\mathworks_tmp_1272_9992_1272.auth'.
Parsing file "d:\My Documents\MATLAB\sound\GUI\SineWave.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\MATLAB\R2008a\toolbox\compiler\deploy\deployprint.m"
(Referenced from: "Compiler Command Line").
Parsing file "C:\MATLAB\R2008a\toolbox\compiler\deploy\printdlg.m"
(Referenced from: "Compiler Command Line").
Generating file "SineWave_main.c".
Generating file "d:\My Documents\MATLAB\sound\GUI\readme.txt".
Generating file "SineWave_mcc_component_data.c".
Executing command: mbuild -O -v -output "SineWave" "SineWave_main.c" "SineWave_mcc_component_data.c" -link exe
This is mbuild Copyright 1984-2004 The MathWorks, Inc.
-> Default options filename found in C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R14
----------------------------------------------------------------
-> Options file = C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R14\compopts.bat
-> COMPILER = lcc
-> Compiler flags:
COMPFLAGS = -c -Zp8 -I"C:\MATLAB\R2008a\sys\lcc\include" -noregistrylookup
OPTIMFLAGS = -DNDEBUG
DEBUGFLAGS = -g4
arguments =
Name switch = -Fo
-> Pre-linking commands =
-> LINKER = lcclnk
-> Link directives:
LINKFLAGS = -tmpdir "." -L"C:\MATLAB\R2008a\sys\lcc\lib" -libpath "C:\MATLAB\R2008a\extern\lib\win32\lcc"
LINKFLAGSPOST = mclmcrrt.lib
Name directive = -o "SineWave.exe"
File link directive =
Lib. link directive =
Rsp file indicator = @
-> Resource Compiler =
-> Resource Linker =
----------------------------------------------------------------
--> "lcc -c -Zp8 -I"C:\MATLAB\R2008a\sys\lcc\include" -noregistrylookup -FoSineWave_main.obj -IC:\MATLAB\R2008a\extern\include -IC:\MATLAB\R2008a\simulink\include -DNDEBUG SineWave_main.c"
--> "lcc -c -Zp8 -I"C:\MATLAB\R2008a\sys\lcc\include" -noregistrylookup -FoSineWave_mcc_component_data.obj -IC:\MATLAB\R2008a\extern\include -IC:\MATLAB\R2008a\simulink\include -DNDEBUG SineWave_mcc_component_data.c"
Contents of 3909_tmp.rsp:
SineWave_main.obj SineWave_mcc_component_data.obj
--> "lcclnk -o "SineWave.exe" -tmpdir "." -L"C:\MATLAB\R2008a\sys\lcc\lib" -libpath "C:\MATLAB\R2008a\extern\lib\win32\lcc" @3909_tmp.rsp mclmcrrt.lib"
--> "if exist _lib3909.def del _lib3909.def"
--> "if exist %LIB_NAME_stub.obj del %LIB_NAME_stub.obj"
>>