MATLAB Compiler    

Coding with M-Files Only

One way to create a stand-alone application is to write all the source code in one or more M-files or MEX-files. Coding an application in M-files allows you to take advantage of the MATLAB interpretive development environment. Then, after getting the M-file version of your program working properly, compile the code and build it into a stand-alone application.

Consider a very simple application whose source code consists of two M-files, mrank.m and main.m. This example involves C code; you use a similar process (described below) for C++ code. In this example, the line r = zeros(n,1) preallocates memory to help the performance of the Compiler.

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:

main.m contains a "main routine" that calls mrank and then prints the results:

To compile these into code that can be built into a stand-alone application, invoke the MATLAB Compiler:

The -m option flag causes the MATLAB Compiler to generate C source code suitable for stand-alone applications. For example, the MATLAB Compiler generates C source code files main.c, main_main.c, and mrank.c. main_main.c contains a C function named main; main.c and mrank.c contain a C functions named mlfMain and mlfMrank. (The -c option flag inhibits invocation of mbuild.)

To build an executable application, you can use mbuild to compile and link these files. Or, you can automate the entire build process (invoke the MATLAB Compiler twice, use mbuild to compile the files with your ANSI C compiler, and link the code) by using the command

Figure 4-2, Building Two M-Files into a Stand-Alone C Application, illustrates the process of building a stand-alone C application from two M-files. The commands to compile and link depend on the operating system being used. See Building Stand-Alone C/C++ Applications for details.

Figure 4-2: Building Two M-Files into a Stand-Alone C Application

For C++ code, add -L cpp to the previous commands and use a C++ compiler instead of a C compiler.


  Troubleshooting the Compiler Alternative Ways of Compiling M-Files