| MATLAB® Compiler™ | ![]() |
| On this page… |
|---|
One way to create a standalone application is to write all the source code in one or more M-files or MEX-files as in the previous magic square example. Coding an application with M allows you to take advantage of the MATLAB® interactive development environment. Once the M-file version of your program works properly, compile the code and build it into a standalone application.
Consider a simple application whose source code consists of two M-files, mrank.m and main.m. This example generates C code from your M-files.
mrank.m returns a vector of integers, r. Each element of r represents the rank of a magic square. For example, after the function completes, r(3) contains the rank of a 3-by-3 magic square.
function r = mrank(n) r = zeros(n,1); for k = 1:n r(k) = rank(magic(k)); end
In this example, the line r = zeros(n,1) preallocates memory to help the performance of the MATLAB® Compiler™ product.
main.m contains a "main routine" that calls mrank and then prints the results.
function main r = mrank(5)
To compile these functions into code that can be built into a standalone application, invoke the MATLAB Compiler product.
mcc -m main mrank
The -m option causes the MATLAB Compiler product to generate C source code suitable for standalone applications. For example, the MATLAB Compiler product generates C source code files main_main.c and main_mcc_component_data.c. main_main.c contains a C function named main; main_mcc_component_data.c contains data needed by the MCR to run the application.
To build an application, you can use mbuild to compile and link these files. Or, you can automate the entire build process (invoke MATLAB Compiler on both M-files, use mbuild to compile the files with your ANSI C compiler, and link the code) by using the command
mcc -m main mrank
If you need to combine other code with your application (Fortran, for example, a language not supported by the MATLAB Compiler product), or if you want to build a makefile that compiles your application, you can use the command
mcc -mc main mrank
The -c option inhibits invocation of mbuild. You will probably need to examine the verbose output of mbuild to determine how to set the compiler options in your makefile. Run
mcc -mv main mrank
to see the switches and options that mbuild uses on your platform.
![]() | C Standalone Application Target | Mixing M-Files and C or C++ | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |