| MATLAB Compiler 2.0 | Search  Help Desk |
| mcc (Compiler 2.0) | Examples |
Syntax
mcc [-options]mfile1[mfile2...mfileN] [C/C++file1 ... C/C++fileN]
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 tomcc. (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:
mcc -m -g myfunYou can group options that do not take arguments by preceding the list of option flags with a single dash (
-), for example:
mcc -mg myfunOptions that take arguments cannot be combined unless you place the option with its arguments last in the list. For example, these formats are valid:
mcc -m -A full myfun % Options listed separately mcc -mA full myfun % Options combined, A option lastThis format is not valid:
mcc -Am full myfun % Options combined, A option not lastIn 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
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.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.-V1.2 option. For example, to generate a C function named main using Compiler 1.2, use:
mcc -V1.2 -m myfuncTo generate a stand-alone C application using Compiler 2.0, use:
mcc -m myfunc
Setting Up Default Options
If you have some command line options that you wish always to pass tomcc, 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:
..$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 anmccpath file. To do this:
. -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.)
.The current working directory, or
$HOME/matlab (UNIX) or <matlab>\bin (PC)
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,mcc -m -W none test.mis equivalent to
mcc -t -W main -L C -T link:exe -h -W none test.mIn 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 themcc command line, the Compiler:
..-I <path> <file>".
For example,
mcc -m /home/user/myfile.mwould be treated as
mcc -m -I /home/user myfile.mIn 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
mcc -m -I /home/user/dir1 /home/user/dir2/myfile.mwould be equivalent to
mcc -m -I /home/user/dir1 -I /home/user/dir2 myfile.mThe 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 thatbell.m calls watson.m. Compiling with
mcc -x bell watsoncreates
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
mcc -x watson bellcreates
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.-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)-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:
mcc -m mymfileThe
-m option is equivalent to the series of options:
-t -W main -L C -T link:exe -h
-p (Stand-Alone C++)-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:
mcc -p mymfileThe
-p option is equivalent to the series of options:
-t -W main -L Cpp -T link:exe -h
-S (Simulink S-Function)mymfile.m into C and to create the corresponding Simulink S-function using dynamically sized inputs and outputs, use:
mcc -S mymfileThe
-S option is equivalent to the series of options:
-t -W simulink -L C -T link:mex
-x (MEX-Function)mymfile.m into C and to create the corresponding MEX-file that can be called directly from MATLAB, use:
mcc -x mymfileThe
-x option is equivalent to the series of options:
-t -W mex -L C -T link:mex
Code Generation Options
-A (Annotation Control for Output Source)annotation)
#line preprocessor directive inclusion (line)
debugline)
mcc -A annotation:typeTable 1-4 shows the available types of code and comment annotation options.
#line preprocessor directives that are included in the generated C/C++ source, use:
mcc -A line:settingTable 1-5 shows the available
#line directive settings.mcc -A debugline:onTable 1-6 shows the available debugline directive settings.
mcc -A annotation:all -A line:on or mcc -A line:on (The default is all for code/comment inclusion.)To include none of your M-code and no
#line preprocessor directives, use:
mcc -A annotation:none -A line:offTo 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:
mcc -A line:on -A debugline:on-F <option> (Formatting)
mcc -A debugline:on-L <language> (Target Language)
C or Cpp. The default is C. Note that these values are case insensitive.
-u (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)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)-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)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)-d option.
-h (Helper Functions)-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
mcc -m minimize_it fminsinstead of
mcc -m -h minimize_it-I <directory> (Directory Path)
-I option adds a directory to the end of the current search path. For example,
-I <directory1> -I <directory2>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>.exe for PC stand-alone applications, .mexsol for Solaris MEX-files).
-t (Translate M to C/C++)target.mex or mbuild
-v option passes the -v option to mex or mbuild and displays information about mex or mbuild.
-V1.2 (MATLAB Compiler 1.2)license.dat file when checking out a Compiler license.
mbuild/mex Options
-f <filename> (Specifying Options File)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)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)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)matlabroot.
Examples
Make a C translation and a MEX-file formyfun.m:
mcc -x myfunMake a C translation and a stand-alone executable for
myfun.m:
mcc -m myfunMake a C++ translation and a stand-alone executable for
myfun.m:
mcc -p myfunMake a C translation and a Simulink S-function for
myfun.m (using dynamically sized inputs and outputs):
mcc -S myfunMake a C translation and a Simulink S-function for
myfun.m (explicitly calling for one input and two outputs):
mcc -S -u 1 -y 2 myfunMake 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:
mcc -m -I /files/source -d /files/target myfunMake 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:
mcc -x -h -A annotation:all myfunMake a generic C translation of
myfun.m:
mcc -t -L C myfunMake a generic C++ translation of
myfun.m:
mcc -t -L Cpp myfunMake a C MEX wrapper file from
myfun1.m and myfun2.m:
mcc -W mex -L C myfun1 myfun2Make a C translation and a stand-alone executable from
myfun1.m and myfun2.m (using one mcc call):
mcc -m myfun1 myfun2Make a C translation and a stand-alone executable from
myfun1.m and myfun2.m (by generating each output file with a separate mcc call):
mcc -t -L C myfun1 % yields myfun1.c mcc -t -L C myfun2 % yields myfun2.c mcc -W main -L C myfun1 myfun2 % yields myfun1_main.c mcc -T compile:exe myfun1.c % yields myfun1.o mcc -T compile:exe myfun2.c % yields myfun2.o mcc -T compile:exe myfun1_main.c % yields myfun1_main.o mcc -T link:exe myfun1.o myfun2.o myfun1_main.oNote On PCs, filenames ending with
.o above would actually end with .obj.