MATLAB Compiler 2.0
  Go to function:
    Search    Help Desk 
mcc (Compiler 2.0)    Examples

Invoke MATLAB Compiler 2.0.

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).

Note Compiler 2.0 is the default compiler. If you want to use Compiler 1.2, you must include the -V1.2 option on the mcc command line. See "mcc (Compiler 1.2)" for the mcc reference page for Compiler 1.2.

Command Line Syntax

You may specify one or more MATLAB Compiler option flags to mcc. (The complete list of option flags appears later in this reference page.) 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 take 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.

Note The -V1.2 option cannot be combined with other options; it must stand by itself. For example, you cannot use

    mcc -V1.2ir myfun

You would use

    mcc -V1.2 -ir myfun

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.

Simplifying the Compilation Process

Compiler 2.0, 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 1-1 shows the relationship between the simple, macro approach to accomplish a standard compilation and the more advanced, multi-option alternative.

Table 1-1: Basic Compiler Option Equivalencies
To build a
Simple
Advanced
Stand-alone C application
-m
-t -W main     -L C   -T link:exe -h
Stand-alone C++ application
-p
-t -W main     -L Cpp -T link:exe -h
MEX-function
-x
-t -W mex      -L C   -T link:mex
Simulink S-function
-S
-t -W simulink -L C   -T link:mex



              
 

The remainder of this reference page provides complete descriptions of these options.

Differences Between Compiler 2.0 and Compiler 1.2 Options

Although most of the Compiler options perform the same functions in Compiler 2.0 and Compiler 1.2, there are several options whose functions differ. In particular, some of the macros described in the previous section, "Simplifying the Compilation Process," behave differently depending on which Compiler you use. Table 1-2 describes the functionality of the macro options in Compiler 2.0 and their corresponding functionality in Compiler 1.2.

Table 1-2: Macro Options  
Option
Compiler 2.0
Compiler 1.2
m
Generates a stand-alone C application.
Generates a C function named main.
p
Generates a stand-alone C++ application.
Generates a stand-alone C++ application when the file name is main.m or a C++ file is specified.
x
Generates a MEX-function.
Did not exist. (It was the default.)
S
Generates a Simulink MEX-function.
Generates a Simulink MEX-function.

To use the Compiler 1.2 version of these options, you must specify the -V1.2 option. For example, to generate a C function named main using Compiler 1.2, use:

To generate a stand-alone C application using Compiler 2.0, use:

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:

   1.
The current working directory, or
   2.
$HOME/matlab (UNIX) or <matlab>\bin (PC)
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:

   1.
Create a text file containing the text -I <your_directory_here> for each directory you want on the default path, and name this file mccpath. (Alternately, you can call the MCCSAVEPATH M-function from MATLAB to create an mccpath file.)
   2.
Place this file in one of two directories:
The stand-alone version of the MATLAB Compiler searches for the mccpath file in these two directories in the order shown above. 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 1-1,

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.

Note Macros and regular options may both affect the same settings and may therefore override each other depending on their order in the command line.

Handling Full Pathnames

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

   1.
Breaks the full name into the corresponding path- and filenames (<path> and <file>).
   2.
Replaces the full pathname in the argument list with "-I <path> <file>". For example,
would be treated as

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

would be equivalent to

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.

Note The Compiler produces a warning (specified_file_mismatch) if a file with a full pathname is included on the command line and it finds it somewhere else.

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 2.0 Option Flags

The MATLAB Compiler option flags perform various functions that affect the generated code and how the Compiler behaves. Table 1-3 shows the categories of the Compiler options.

Table 1-3: Compiler Option Categories  
Category
Purpose
Macros
The macro options simplify the compilation process by combining the most common compilation tasks into single options.
Code Generation
These options affect the actual code that the Compiler generates. For example, -L specifies the target language as either C or C++.
Compiler and Environment
These options provide information to the Compiler such as where to put (-d) and find (-I) particular files.
mbuild/mex
These options provide information for the mbuild and/or mex scripts.

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.

Note If you use the option flags that optimize the generated code, you must use the -V1.2 option, making Compiler 1.2 the active compiler. The -V1.2 option is not supported in the stand-alone Compiler.

Macro Options

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

-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:

-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:

-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:

-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:

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 1-4 shows the available types of code and comment annotation options.

Table 1-4: Code/Comment Annotation Options
type
Description
all
Provides the complete source of the M-file interleaved with the generated C/C++ source. The default is all.
comments
Provides all of the comments from the M-file interleaved with the generated C/C++ source.
none
No comments or code from the M-file are added to code.

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

Table 1-5 shows the available #line directive settings.

Table 1-5: Line Annotation Options
Setting
Description
on
Adds #line preprocessor directives to the generated
C/C++ source code to enable source M-file debugging.
off
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 1-6 shows the available debugline directive settings.

Table 1-6: Run-Time Error Annotation Options
Setting
Description
on
Specifies the presence of source file and line number information in run-time error messages.
off
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 1-7 shows the available options.

Table 1-7: Formatting Options  
<Option>
Description
list
Generates a table of all the available formatting options.
expression-indent:n
Sets the number of spaces of indentation for all expressions to n, where n is an integer. The default indent is 4.
page-width:n
Sets maximum width of generated code to n, where n is an integer. The default width is 80.
statement-indent:n
Sets the number of spaces of indentation for all statements to n, where n is an integer. The default indent is 2.

-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.

-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. (Used with -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 1-8 shows the valid type options.

Table 1-8: Function Wrapper Types  
<Type>
Description
mex
Produces a mexFunction() interface.
main
Produces a POSIX shell main() function.
simulink
Produces a Simulink C MEX S-function interface.
lib:<string>
Produces an initialization and termination function for use when compiling this Compiler-generated code into a larger application. This option also produces a header file containing prototypes for all public functions in all M-files specified. <string> becomes the base (file) name for the generated C/C++ and header file. Creates a .exports file that contains all non-static function names.
none
Does not produce a wrapper file. none is the default.

Caution When generating function wrappers, you must specify all M-files that are being linked together on the command line. These files are used to produce the initialization and termination functions as well as global variable definitions. If the functions are not specified in this manner, undefined symbols will be produced at link time.

-y (Number of Outputs)
Provides 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. (Used with -S option.)

Compiler and Environment Options

-B <filename> (Bundle of Compiler Settings)
Replace -B <filename> 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.You can place options that you always set in an mccstartup file. For more information, see "Setting Up Default Options."

-c (C Code Only)
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 by default. 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 <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 (MEX-file or application) of the Compiler. A suitable, possibly platform-dependent, extension is added to the specified basename (e.g., .exe for PC stand-alone applications, .mexsol for Solaris MEX-files).

-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 1-9 gives the possible values of target.

Table 1-9: Output Stage Options  
<Target>
Description
codegen
Translates M-files to C/C++ files. The default is codegen.
compile:<bin>
Translates M-files to C/C++ files; compiles to object form.
link:<bin>
Translates M-files to C/C++ files; compiles to object form; links to executable form (MEX or stand-alone application.)
    where <bin> can be mex, exe, or lib. mex uses the mex script to build a MEX-file; exe uses the mbuild script to build an executable; lib uses mbuild to build a shared library.

-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.

-V1.2 (MATLAB Compiler 1.2)
Invoke the MATLAB Compiler 1.2. This option is not supported in the stand-alone Compiler mode; it works only from the MATLAB prompt. If you obtained good optimization from Compiler 1.2, this option enables you to continue to benefit from the performance advantages of that Compiler. For more information about the MATLAB Compiler 1.2 options, see the "mcc (Compiler 1.2)" reference page.

-V2.0 (MATLAB Compiler 2.0)
Invoke the MATLAB Compiler 2.0. This option works from both the MATLAB prompt and the DOS or UNIX command line.

-w (Warning)
Display warning messages. Table 1-10 shows the various ways you can use the -w option.

Table 1-10: Warning Option  
Syntax
Description
(no -w option)
Default; displays only serious warnings.
-w list
Generates a table that maps <string> to warning message for use with enable, disable, and error. Appendix B lists the same information.
-w
Enables complete warnings.
-w disable[:<string>]
Disables specific warning associated with <string>. Appendix B lists the valid <string> values. Leave off the optional :<string> to apply the disable action to all warnings.
-w enable[:<string>]
Enables specific warning associated with <string>. Appendix B lists the valid <string> values. Leave off the optional :<string> to apply the enable action to all warnings.
-w error[:<string>]
Treats specific warning associated with <string> as error. Leave off the optional :<string> to apply the error action to all warnings.

-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 the Application Program Interface Guide for more information about using this option with the mex script.

Note Although this option works as documented, it is suggested that you use mex -setup or mbuild -setup to switch compilers.

-g (Debugging Information)
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.

The -g option flag has no influence on the source code that the MATLAB Compiler generates, though it does have some influence on the binary code that the C/C++ compiler generates.

-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".

Note Multiple -M options do not accumulate; only the last -M option is used.

-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 by matlabroot.

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):

Note On PCs, filenames ending with .o above would actually end with .obj.



[ Previous | Help Desk | Next ]