| MATLAB® Compiler™ | ![]() |
| On this page… |
|---|
This example takes an M-file, magicsquare.m, and creates a standalone C application, magicsquare.
Copy the file magicsquare.m from
matlabroot/extern/examples/compiler
to your work directory.
mcc -mv magicsquare.m
The -m option tells MATLAB Compiler (mcc) to generate a C standalone application. The -v option (verbose) displays the compilation steps throughout the process and helps identify other useful information such as which third-party compiler is used and what environment variables are referenced.
This command creates the standalone application called magicsquare and additional files. The Windows platform appends the .exe extension to the name. See the table in Standalone Executable for the complete list of files created.
These steps test your standalone application on your development machine.
Note Testing your application on your development machine is an important step to help ensure that your application is compilable. To verify that your application compiled properly, you must test all functionality that is available with the application. If you receive an error message similar to Undefined function or Attempt to execute script script_name as a function, it is likely that the application will not run properly on deployment machines. Most likely, your CTF archive is missing some necessary functions. Use -a to add the missing functions to the archive and recompile your code. |
Windows. Add the following directory to your path.
matlabroot\bin\win32
UNIX. Add the following platform-specific directories to your dynamic library path.
Note For readability, the following commands appear on separate lines, but you must enter each setenv command on one line. |
Linux
setenv LD_LIBRARY_PATH matlabroot/sys/os/glnx86: matlabroot/bin/glnx86: matlabroot/sys/java/jre/glnx86/jre/lib/i386/native_threads: matlabroot/sys/java/jre/glnx86/jre/lib/i386/server: matlabroot/sys/java/jre/glnx86/jre/lib/i386: setenv XAPPLRESDIR matlabroot/X11/app-defaults
Solaris 64
setenv LD_LIBRARY_PATH /usr/lib/lwp: matlabroot/sys/os/sol64: matlabroot/bin/sol64: matlabroot/sys/java/jre/sol64/jre1.5.0/lib/sparcv9/native_threads: matlabroot/sys/java/jre/sol64/jre1.5.0/lib/sparcv9/server: matlabroot/sys/java/jre/sol64/jre1.5.0/lib/sparcv9: setenv XAPPLRESDIR matlabroot/X11/app-defaults
Linux x86-64
setenv LD_LIBRARY_PATH matlabroot/sys/os/glnxa64: matlabroot/bin/glnxa64: matlabroot/extern/lib/glnxa64: matlabroot/sys/java/jre/glnxa64/jre/lib/amd64/native_threads: matlabroot/sys/java/jre/glnxa64/jre/lib/amd64/server: matlabroot/sys/java/jre/glnxa64/jre/lib/amd64: setenv XAPPLRESDIR matlabroot/X11/app-defaults
Mac OS X
setenv DYLD_LIBRARY_PATH matlabroot/bin/mac: matlabroot/sys/os/mac: /System/Library/Frameworks/JavaVM.framework/JavaVM: /System/Library/Frameworks/JavaVM.framework/Libraries setenv XAPPLRESDIR matlabroot/X11/app-defaults
Intel Mac (Maci)
setenv DYLD_LIBRARY_PATH matlabroot/bin/maci: matlabroot/sys/os/maci: /System/Library/Frameworks/JavaVM.framework/JavaVM: /System/Library/Frameworks/JavaVM.framework/Libraries setenv XAPPLRESDIR matlabroot/X11/app-defaults
Run the standalone application from the system prompt (shell prompt on UNIX or DOS prompt on Windows) by typing the application name.
magicsquare.exe 4 (On Windows) magicsquare 4 (On UNIX)
The results are:
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
You can distribute a MATLAB Compiler generated standalone application to any target machine that has the same operating system as the machine on which the application was compiled.
For example, if you want to deploy an application to a Windows machine, you must use MATLAB Compiler to build the application on a Windows machine. If you want to deploy the same application to a UNIX machine, you must use MATLAB Compiler on the same UNIX platform and completely rebuild the application. To deploy an application to multiple platforms requires MATLAB and MATLAB Compiler licenses on all the desired platforms.
Note MCRInstaller.exe has obsoleted the need for the function buildmcr or the creation of MCRInstaller.zip. See Replacement of MCRInstaller.zip and BUILDMCR Functionality for more details including complete file paths to all install programs. |
Gather and package the following files and distribute them to the deployment machine.
Component | Description |
|---|---|
MCRInstaller.exe | Self-extracting MATLAB Compiler Runtime library utility; platform-dependent file that must correspond to the end user's platform. |
magicsquare | Application; magicsquare.exe for Windows |
Distribute and package your standalone application on UNIX by packaging the following files and distributing them to the deployment machine.
Component | Description |
|---|---|
MCRInstaller.bin | MATLAB Compiler Runtime library archive; platform-dependent file that must correspond to the end user's platform |
magicsquare | Application |
These steps describe the process that end users must follow to install and run the application on their machines.
Install the MCR by running the MCRInstaller in a directory. For example, run MCRInstaller.exe in C:\MCR. For more information on running the MCR Installer utility, seeWorking with the MCR and Replacement of MCRInstaller.zip and BUILDMCR Functionality.
Note For readability, the following commands appear on separate lines, but you must enter each setenv command on one line. On Windows XP, this directory is automatically added to your path. |
Windows
<mcr_root>\<ver>\runtime\win32
Linux
setenv LD_LIBRARY_PATH <mcr_root>/<ver>/runtime/glnx86: <mcr_root>/<ver>/sys/os/glnx86: <mcr_root>/<ver>/sys/java/jre/glnx86/jre/lib/i386/native_threads: <mcr_root>/<ver>/sys/java/jre/glnx86/jre/lib/i386/server: <mcr_root>/<ver>/sys/java/jre/glnx86/jre/lib/i386: setenv XAPPLRESDIR <mcr_root>/<ver>/X11/app-defaults
Solaris 64
setenv LD_LIBRARY_PATH /usr/lib/lwp: <mcr_root>/<ver>/runtime/sol64: <mcr_root>/<ver>/sys/os/sol64: <mcr_root>/<ver>/sys/java/jre/sol64/jre1.5.0/lib/sparcv9/native_threads: <mcr_root>/<ver>/sys/java/jre/sol64/jre1.5.0/lib/sparcv9/server: <mcr_root>/<ver>/sys/java/jre/sol64/jre1.5.0/lib/sparcv9: setenv XAPPLRESDIR <mcr_root>/<ver>/X11/app-defaults
Linux x86-64
setenv LD_LIBRARY_PATH <mcr_root>/<ver>/runtime/glnxa64: <mcr_root>/<ver>/sys/os/glnxa64: <mcr_root>/<ver>/sys/java/jre/glnxa64/jre/lib/amd64/native_threads: <mcr_root>/<ver>/sys/java/jre/glnxa64/jre/lib/amd64/server: <mcr_root>/<ver>/sys/java/jre/glnxa64/jre/lib/amd64: setenv XAPPLRESDIR <mcr_root>/<ver>/X11/app-defaults
Mac OS X
setenv DYLD_LIBRARY_PATH <mcr_root>/<ver>/runtime/mac: <mcr_root>/<ver>/sys/os/mac: <mcr_root>/<ver>/bin/mac: /System/Library/Frameworks/JavaVM.framework/JavaVM: /System/Library/Frameworks/JavaVM.framework/Libraries setenv XAPPLRESDIR <mcr_root>/<ver>/X11/app-defaults
Intel Mac (Maci)
setenv DYLD_LIBRARY_PATH <mcr_root>/version/runtime/maci: <mcr_root>/version/sys/os/maci: <mcr_root>/version/bin/maci: /System/Library/Frameworks/JavaVM.framework/JavaVM: /System/Library/Frameworks/JavaVM.framework/Libraries setenv XAPPLRESDIR <mcr_root>/version/X11/app-defaults
Note If you are running a version of Intel Mac later than 10.3, use the bsh, rather than the csh, shell. |
Caution There is a limitation regarding directories on your path. If the target machine has a MATLAB installation, the <mcr_root> directories must be first on the path to run the deployed application. To run MATLAB, the matlabroot directories must be first on the path. This restriction only applies to configurations involving an installed MCR and an installed MATLAB on the same machine. |
Run the magicsquare standalone application from the system prompt and provide a number representing the size of the desired magic square, for example, 4.
magicsquare 4
The results are displayed as:
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Note Input arguments you pass to and from a system prompt are treated as string input and you need to consider that in your application. For more information, see Passing Arguments to and from a Standalone Application. |
Note Before executing your MATLAB Compiler generated executable, set the LD_PRELOAD environment variable to /lib/libgcc_s.so.1. |
![]() | Introduction | Coding with M-Files Only | ![]() |
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |