compile

MATLAB Compiler

mcc 

Invoke MATLAB Compiler

Syntax

Description

mcc is the MATLAB command that invokes the MATLAB Compiler. You can issue the mcc command either from the MATLAB command prompt (MATLAB mode) or the DOS or UNIX command line (stand-alone mode).

Command Line Syntax

You may specify one or more MATLAB Compiler option flags to mcc. Most option flags have a one-letter name. You can list options separately on the command line, for example:

You can group options that do not take arguments by preceding the list of option flags with a single dash (-), for example:

Options that take arguments cannot be combined unless you place the option with its arguments last in the list. For example, these formats are valid:

This format is not valid:

In cases where you have more than one option that takes arguments, you can only include one of those options in a combined list and that option must be last. You can place multiple combined lists on the mcc command line.

If you include any C or C++ filenames on the mcc command line, the files are passed directly to mex or mbuild, along with any Compiler-generated C or C++ files.

Using Macros to Simplify Compilation

The MATLAB Compiler, through its exhaustive set of options, gives you access to the tools you need to do your job. If you want a simplified approach to compilation, you can use one simple option, i.e., macro, that allows you to quickly accomplish basic compilation tasks. If you want to take advantage of the power of the Compiler, you can do whatever you desire to do by choosing various Compiler options.

Table 7-2, Macro Options, shows the relationship between the macro approach to accomplish a standard compilation and the multioption alternative.

Understanding a Macro Option.   The -m option tells the Compiler to produce a stand-alone C application. The -m macro is equivalent to the series of options

Table 7-3, -m Macro, shows the options that compose the -m macro and the information that they provide to the Compiler.

Changing Macro Options.   You can change the meaning of a macro option by editing the corresponding macro_option file bundle file in <matlab>/toolbox/compiler/bundles. For example, to change the -x macro, edit the file macro_option_x in the bundles directory.

Setting Up Default Options

If you have some command line options that you wish always to pass to mcc, you can do so by setting up an mccstartup file. Create a text file containing the desired command line options and name the file mccstartup. Place this file in one of two directories:

mcc searches for the mccstartup file in these two directories in the order shown above. If it finds an mccstartup file, it reads it and processes the options within the file as if they had appeared on the mcc command line before any actual command line options. Both the mccstartup file and the -B option are processed the same way.

Setting a MATLAB Path in the Stand-Alone MATLAB Compiler

Unlike the MATLAB version of the Compiler, which inherits a MATLAB path from MATLAB, the stand-alone version has no initial path. If you want to set up a default path, you can do so by making an mccpath file. To do this:

The stand-alone version of the MATLAB Compiler searches for the mccpath file in your current directory and then your preferences directory. If it finds an mccpath file, it processes the directories specified within the file and uses them to initialize its search path. Note that you may still use the -I option on the command line or in mccstartup files to add other directories to the search path. Directories specified this way are searched after those directories specified in the mccpath file.

Conflicting Options on Command Line

If you use conflicting options, the Compiler resolves them from left to right, with the rightmost option taking precedence. For example, using the equivalencies in Table 7-2, Macro Options,

is equivalent to

In this example, there are two conflicting -W options. After working from left to right, the Compiler determines that the rightmost option takes precedence, namely, -W none, and the Compiler does not generate a wrapper.

Handling Full Pathnames

If you specify a full pathname to an M-file on the mcc command line, the Compiler:

In rare situations, this behavior can lead to a potential source of confusion. For example, suppose you have two different M-files that are both named myfile.m and they reside in/home/user/dir1 and /home/user/dir2. The command

The Compiler finds the myfile.m in dir1 and compiles it instead of the one in dir2 because of the behavior of the -I option. If you are concerned that this might be happening, you can specify the -v option and then see which M-file the Compiler parses. The -v option prints the full pathname to the M-file.

Compiling Embedded M-Files

If the M-file you are compiling calls other M-files, you can list the called M-files on the command line. Doing so causes the MATLAB Compiler to build all the M-files into a single MEX-file, which usually executes faster than separate MEX-files. Note, however, that the single MEX-file has only one entry point regardless of the number of input M-files. The entry point is the first M-file on the command line. For example, suppose that bell.m calls watson.m. Compiling with

creates bell.mex. The entry point of bell.mex is the compiled code from bell.m. The compiled version of bell.m can call the compiled version of watson.m. However, compiling as

creates watson.mex. The entry point of watson.mex is the compiled code from watson.m. The code from bell.m never gets executed.

As another example, suppose that x.m calls y.m and that y.m calls z.m. In this case, make sure that x.m is the first M-file on the command line. After x.m, it does not matter which order you specify y.m and z.m.

MATLAB Compiler Option Flags

The MATLAB Compiler option flags perform various functions that affect the generated code and how the Compiler behaves. Table 7-4, Compiler Option Categories, shows the categories of options.

The remainder of this reference page is subdivided into sections that correspond to the Compiler option categories. Each section provides a full description of all of the options in the category.

Macro Options

The macro options provide a simplified way to accomplish basic compilation tasks.

-g (Debug).   This option is a macro that is equivalent to

or

In addition to the -G option, the -g option includes the -A debugline:on option. This will have an impact on performance of the generated code. If you want to have debugging information, but do not want the performance degradation associated with the debug line information, use -g -A debugline:off. The -g option also includes the -O none option, causing all compiler optimizations to be turned off. If you want to have some optimizations on, you may specify them after the debug option.

-m (Stand-Alone C).   Produce a stand-alone C application. It includes helper functions by default (-h), and then generates a stand-alone C wrapper (-W main). In the final stage, this option compiles your code into a stand-alone executable and links it to the MATLAB C/C++ Math Library (-T link:exe). For example, to translate an M-file named mymfile.m into C and to create a stand-alone executable that can be run without MATLAB, use

The -m option is equivalent to the series of options

or

-p (Stand-Alone C++).   Produce a stand-alone C++ application. It includes helper functions by default (-h), and then generates a stand-alone C++ wrapper (-W main). In the final stage, this option compiles your code into a stand-alone executable and links it to the MATLAB C/C++ Math Library (-T link:exe). For example, to translate an M-file named mymfile.m into C++ and to create a stand-alone executable that can be run without MATLAB, use

The -p option is equivalent to the series of options

or

-S (Simulink S-Function).   Produce a Simulink S-function that is compatible with the Simulink S-Function block. For example, to translate an M-file named mymfile.m into C and to create the corresponding Simulink S-function using dynamically sized inputs and outputs, use

The -S option is equivalent to the series of options

or

-x (MEX-Function).   Produce a MEX-function. For example, to translate an M-file named mymfile.m into C and to create the corresponding MEX-file that can be called directly from MATLAB, use

The -x option is equivalent to the series of options

or

Bundle Files

-B ccom (C COM Object).   Produce a C COM object. The -B ccom option is equivalent to the series of options

-B cexcel (C Excel COM Object).   Produce a C Excel COM object. The -B cexcel option is equivalent to the series of options

-B csglcom (C Handle Graphics COM Object).   Produce a C COM object that uses Handle Graphics. The -B csglcom option is equivalent to the series of options

-B csglexcel (C Handle Graphics Excel COM Object).   Produce a C Excel COM object that uses Handle Graphics. The -B csglexcel option is equivalent to the series of options

-B csglsharedlib (C Handle Graphics Shared Library).   Produce a C shared library that uses Handle Graphics. The -B csglsharedlib option is equivalent to the series of options

-B cppcom (C++ COM Object).   Produce a C++ COM object. The -B cppcom option is equivalent to the series of options

-B cppexcel (C++ Excel COM Object).   Produce a C++ Excel COM object. The -B cppexcel option is equivalent to the series of options

-B cppsglcom (C++ Handle Graphics COM Object).   Produce a C++ COM object that uses Handle Graphics. The -B cppsglcom option is equivalent to the series of options

-B cppsglexcel (C++ Handle Graphics Excel COM Object).   Produce a C++ Excel COM object that uses Handle Graphics. The -B cppsglexcel option is equivalent to the series of options

-B cpplib (C++ Library).   Produce a C++ library. The -B cpplib option is equivalent to the series of options

-B csharedlib (C Shared Library).   Produce a C shared library. The -B csharedlib option is equivalent to the series of options

-B pcode (MATLAB P-Code).   Produce MATLAB P-code.

The -B pcode option is equivalent to the series of options

-B sgl (Stand-Alone C Graphics Library).   Produce a stand-alone C application that uses Handle Graphics.

The -B sgl option is equivalent to the series of options

-B sglcpp (Stand-Alone C++ Graphics Library).   Produce a stand-alone C++ application that uses Handle Graphics.

The -B sglcpp option is equivalent to the series of options

Code Generation Options

-A (Annotation Control for Output Source).   Control the type of annotation in the resulting C/C++ source file. The types of annotation you can control are

To control the M-file code that is included in the generated C/C++ source, use

Table 7-5, Code/Comment Annotation Options, shows the available annotation options.

To control the #line preprocessor directives that are included in the generated C/C++ source, use

Table 7-6, Line Annotation Options, shows the available #line directive settings.

Table 7-6: Line Annotation Options

Setting

on

off

Description

Adds #line preprocessor directives to the generated 

C/C++ source code to enable source M-file debugging. Note: The page width option is ignored when this is on.

Adds no #line preprocessor directives to the generated C/C++ source code. The default is off.

To control if run-time error messages report the source file and line number, use

Table 7-7, Run-Time Error Annotation Options, shows the available debugline directive settings.

Table 7-7: Run-Time Error Annotation Options

Setting

on

off

Description

Specifies the presence of source file and line number information in run-time error messages.

Specifies no source file and line number information in run-time error messages. The default is off.

For example, to include all of your M-code, including comments, in the generated file and the standard #line preprocessor directives, use

To include none of your M-code and no #line preprocessor directives, use

To include the standard #line preprocessor directives in your generated C/C++ source code as well as source file and line number information in your run-time error messages, use

-F <option> (Formatting).   Control the formatting of the generated code. Table 7-8, Formatting Options, shows the available options.

-l (Line Numbers) .   Generate C/C++ code that prints filename and line numbers on run-time errors. This option flag is useful for debugging, but causes the executable to run slightly slower. This option is equivalent to

-L <language> (Target Language).   Specify the target language of the compilation. Possible values for language are C or Cpp. The default is C. Note that these values are case insensitive.

-O <option> (Optimization Options).   Optimize your M-file source code so that the performance of the generated C/C++ code may be faster than the performance of the M-code in the MATLAB interpreter. Table 7-9, Optimization Options, shows the available options.

-u (Number of Inputs).   Provide more control over the number of valid inputs for your Simulink S-function. This option specifically sets the number of inputs (u) for your function. If -u is omitted, the input will be dynamically sized. (Use this with the -S option.)

-W <type> (Function Wrapper).   Control the generation of function wrappers for a collection of Compiler-generated M-files. You provide a list of functions and the Compiler generates the wrapper functions and any appropriate global variable definitions. Table 7-10, Function Wrapper Types, shows the valid options.

-y (Number of Outputs).   Provide more control over the number of valid outputs for your Simulink S-function. This option specifically sets the number of outputs (y) for your function. If-y is omitted, the output will be dynamically sized. (Use this with the -S option.)

Compiler and Environment Options

-b (Visual Basic File).   Generate a Visual Basic file (.bas) that contains the Microsoft Excel Formula Function interface to the Compiler-generated COM object.When imported into the workbook Visual Basic code, this code allows the MATLAB function to be seen as a cell formula function.

-B <filename>:[<a1>,<a2>,...,<an>] (Bundle of Compiler Settings).   Replace -B <filename>:[<a1>,<a2>,...,<an>] on the mcc command line with the contents of the specified file. The file should contain only mcc command line options and corresponding arguments and/or other filenames. The file may contain other -B options.

A bundle file can include replacement parameters for Compiler options that accept names and version numbers. For example, there is a bundle file for C shared libraries, csharedlib, that consists of

To invoke the Compiler to produce a C shared library using this bundle, you would use

In general, each %n% in the bundle file will be replaced with the corresponding option specified to the bundle file. Use %% to include a % character. It is an error to have too many or too few options to the bundle file.

You can place options that you always set in an mccstartup file. For more information, see Setting Up Default Options.

This table shows the available bundle files.

-c (C Code Only).   When used with a macro option, generate C code but do not invoke mex or mbuild, i.e., do not produce a MEX-file or stand-alone application. This is equivalent to -T codegen placed at the end of the mcc command line.

-d <directory> (Output Directory).   Place the output files from the compilation in the directory specified by the -d option.

-h (Helper Functions).   Compile helper functions. Any helper functions that are called will be compiled into the resulting MEX or stand-alone application. The -m option automatically compiles all helper functions, so -m effectively calls -h.

Using the -h option is equivalent to listing the M-files explicitly on the mcc command line.

The -h option purposely does not include built-in functions or functions that appear in the MATLAB M-File Math Library portion of the C/C++ Math Libraries. This prevents compiling functions that are already part of the C/C++ Math Libraries. If you want to compile these functions as helper functions, you should specify them explicitly on the command line. For example, use

instead of

-i (Include Exported Interfaces).   Cause the Compiler to include only the M-files that are specified on the command line as exported interfaces. If additional M-files are compiled as a result of being located by the -h option, they are not included in the exported interface that is produced by the MATLAB Compiler.

-I <directory> (Directory Path).   Add a new directory path to the list of included directories. Each -I option adds a directory to the end of the current search path. For example,

would set up the search path so that directory1 is searched first for M-files, followed by directory2. This option is important for stand-alone compilation where the MATLAB path is not available.

-o <outputfile>.   Specify the basename of the final executable output (stand-alone applications only) of the Compiler. A suitable, possibly platform-dependent, extension is added to the specified basename (e.g., .exe for PC stand-alone applications).

-t (Translate M to C/C++).   Translate M-files specified on the command line to C/C++ files.

-T <target> (Output Stage).   Specify the desired output stage. Table 7-11, Output Stage Options, gives the possible values of target.

-v (Verbose).   Display the steps in compilation, including

The -v option passes the -v option to mex or mbuild and displays information about mex or mbuild.

-w (Warning).   Display warning messages. Table 7-12, Warning Option, shows the various ways you can use the -w option.

-Y <license.dat File>.   Use license information in license.dat file when checking out a Compiler license.

mbuild/mex Options

-f <filename> (Specifying Options File).   Use the specified options file when calling mex or mbuild. This option allows you to use different compilers for different invocations of the MATLAB Compiler. This option is a direct pass-through to the mex or mbuild script. See External Interfaces/API in the MATLAB documentation for more information about using this option with the mex script.

-G (Debug Only).   Cause mex or mbuild to invoke the C/C++ compiler with the appropriate C/C++ compiler options for debugging. You should specify -G if you want to debug the MEX-file or stand-alone application with a debugger.

-M "string" (Direct Pass Through).   Pass string directly to the mex or mbuild script. This provides a useful mechanism for defining compile-time options, e.g., -M "-Dmacro=value".

-z <path> (Specifying Library Paths).   Specify the path to use for library and include files. This option uses the specified path for compiler libraries instead of the path returned bymatlabroot.

Examples

Make a C translation and a MEX-file for myfun.m:

Make a C translation and a stand-alone executable for myfun.m:

Make a C++ translation and a stand-alone executable for myfun.m:

Make a C translation and a Simulink S-function for myfun.m (using dynamically sized inputs and outputs):

Make a C translation and a Simulink S-function for myfun.m (explicitly calling for one input and two outputs):

Make a C translation and stand-alone executable for myfun.m. Look for myfun.m in the /files/source directory, and put the resulting C files and executable in the /files/target directory:

Make a C translation and a MEX-file for myfun.m. Also translate and include all M-functions called directly or indirectly by myfun.m. Incorporate the full text of the original M-files into their corresponding C files as C comments:

Make a generic C translation of myfun.m:

Make a generic C++ translation of myfun.m:

Make a C MEX wrapper file from myfun1.m and myfun2.m:

Make a C translation and a stand-alone executable from myfun1.m and myfun2.m (using one mcc call):

Make a C translation and a stand-alone executable from myfun1.m and myfun2.m (by generating each output file with a separate mcc call):

Compile plus1.m into an Excel add-in:

 mbuild

MATLAB Compiler Quick Reference