MATLAB Compiler The Language of Technical ComputingComputationVisualizationProgrammingUser’s GuideVersion 3
viii Contents
4 Stand-Alone Applications4-24Note The MATLAB add-in for Visual Studio does not currently work with Microsoft Visual C/C++, Version 7.0.The add-in fo
Building Stand-Alone Applications on PCs4-25your MATLAB path. If you add directories to your MATLAB path and want them to be visible to the MATLAB add
4 Stand-Alone Applications4-26• See the MATLABAddin.hlp file in the <matlab>\bin\win32 directory, or• Click on the Help icon in the MATLAB add-i
Distributing Stand-Alone Applications4-27Distributing Stand-Alone ApplicationsTo make packaging an application easier, all the necessary MATLAB run-ti
4 Stand-Alone Applications4-28Note If customers already have the MATLAB math and graphics run-time libraries installed on their system, they do not n
Distributing Stand-Alone Applications4-29The ordinal #### could not be located in the dynamic-link library dforrt.dll.To fix this problem, locate dfor
4 Stand-Alone Applications4-30Building Shared LibrariesYou can use mbuild to build C shared libraries on both UNIX and the PC. All of the mbuild optio
Building COM Objects4-31Building COM ObjectsNote To create COM components from the MATLAB Compiler, you must have the MATLAB COM Builder installed on
4 Stand-Alone Applications4-32Building Excel Plug-InsNote To create Excel plug-ins from the MATLAB Compiler, you must have the MATLAB Excel Builder i
Troubleshooting4-33TroubleshootingTroubleshooting mbuildThis section identifies some of the more common problems that might occur when configuring mbu
PrefaceThis chapter provides information about this documentation set. The sections are as follows.Related Products (p. x) MathWorks products related
4 Stand-Alone Applications4-34mbuild Works from Shell but Not from MATLAB (UNIX). If the commandmbuild ex1.cworks from the UNIX command prompt but doe
Troubleshooting4-35Troubleshooting the CompilerTypically, problems that occur when building stand-alone C and C++ applications involve mbuild. However
4 Stand-Alone Applications4-36Coding with M-Files OnlyOne way to create a stand-alone application is to write all the source code in one or more M-fil
Coding with M-Files Only4-37The -m option flag causes the MATLAB Compiler to generate C source code suitable for stand-alone applications. For example
4 Stand-Alone Applications4-38Figure 4-2: Building Two M-Files into a Stand-Alone C Applicationmrank.mmcc -t mrank.mmrank.cC CompilerObject FileC Com
Coding with M-Files Only4-39For C++ code, add -L cpp to the previous commands and use a C++ compiler instead of a C compiler.
4 Stand-Alone Applications4-40Alternative Ways of Compiling M-FilesThe previous section showed how to compile main.m and mrank.m separately. This sect
Alternative Ways of Compiling M-Files4-41mcc -m main_main.c main.c rank.c mrank.cThe resulting stand-alone application uses your customized version of
4 Stand-Alone Applications4-42Mixing M-Files and C or C++The examples in this section illustrate how to mix M-files and C or C++ source code files:• T
Mixing M-Files and C or C++4-43The Build ProcessThe steps needed to build this stand-alone application are1 Compile the M-code.2 Generate the library
PrefacexRelated ProductsThe MATLAB Compiler automatically converts MATLAB M-files to C and C++ code. The MATLAB Compiler includes the MATLAB C/C++ Ma
4 Stand-Alone Applications4-44Figure 4-3: Mixing M-Files and C Code to Form a Stand-Alone Applicationmrank.mmcc -t -W lib:Pkg -T link:exe mrank mrank
Mixing M-Files and C or C++4-45mrankp.cThe code in mrankp.c calls mrank and outputs the values that mrank returns:/* * MRANKP.C * "Posix" C
4 Stand-Alone Applications4-46 /* Print the results. */ mlfPrintMatrix(R); /* Free the matrices allocated during this computation. */
Mixing M-Files and C or C++4-47mlfPrintMatrix is one of the many routines in the MATLAB Math Built-In Library, which is part of the MATLAB Math Librar
4 Stand-Alone Applications4-48 mxArray *a, *b, *x, *y; double x_pr[ROWS * COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; double x_pi[ROWS * COLS] =
Mixing M-Files and C or C++4-49You can build this program into a stand-alone application by using the commandmcc -t -W lib:multpkg -T link:exe multarg
4 Stand-Alone Applications4-50The mlfMultarg function returns matrices a and b. a has both real and imaginary components; b is a scalar having only a
5ControllingCode GenerationThis chapter describes the code generated by the MATLAB Compiler and the options that you can use to control code generati
5 Controlling Code Generation5-2Code Generation OverviewExample M-FilesTo generate the various files created by the Compiler, this chapter uses severa
Code Generation Overview5-3b = y;endfun M-Filefunction a = fun(b)a(1) = b(1) .* b(1);a(2) = b(1) + b(2);a(3) = b(2) / 4;sample M-Filefunction y = samp
Using this GuidexiUsing this GuideThis book describes the MATLAB Compiler and provides numerous examples of how to use it. The topics included are• In
5 Controlling Code Generation5-4 Note Many of the code snippets generated by the MATLAB Compiler that are used in this chapter use the -F page-width
Compiling Private and Method Functions5-5Compiling Private and Method FunctionsPrivate functions are functions that reside in subdirectories with the
5 Controlling Code Generation5-6This table lists the functions you can specify on the command line and their corresponding function and filenames.For
Compiling Private and Method Functions5-7Since it is ambiguous which foo.m you are requesting, it generates the warningWarning: The specified private
5 Controlling Code Generation5-8The Generated Header FilesThis section highlights the two header files that the Compiler can generate for the Sierpins
The Generated Header Files5-9extern mxArray * mlfGasket(mxArray * numPoints);extern void mlxGasket(int nlhs, mxArray * plhs[],
5 Controlling Code Generation5-10extern mwArray gasket(mwArray numPoints = mwArray::DIN);#ifdef __cplusplusextern "C"#endifvoid mlxGasket(in
Internal Interface Functions5-11Internal Interface FunctionsThis section uses the Sierpinski Gasket example (gasket.m) to show several of the generate
5 Controlling Code Generation5-12 */void mlxGasket(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
Internal Interface Functions5-13/* ------------- Output Argument Processing ----------- */ mlfRestorePreviousContext(0, 1, mprhs[0]); plhs[0] =
PrefacexiiTypographical ConventionsThis manual uses some or all of these conventions.Item Convention ExampleExample code Monospace font To assign the
5 Controlling Code Generation5-14This is the corresponding mlfNF interface function (mlfNFoo) for the foo.m example described earlier in this chapter.
Internal Interface Functions5-15mlfVF Interface FunctionThe Compiler produces this interface function only when the M-function uses the variable nargo
5 Controlling Code Generation5-16C++ Interface FunctionsThe C++ interface functions process any input arguments and pass them to the implementation ve
Internal Interface Functions5-17 int i;mclCppUndefineArrays(1, mplhs); if (nlhs > 1) { error( mwVarargin(
5 Controlling Code Generation5-18//// The function "gasket" contains the normal interface for// the "gasket" M-function from file/
Internal Interface Functions5-19// function, appearing above.//mwArray Nfoo(int nargout, mwArray * b, mwArray x, m
5 Controlling Code Generation5-20VF Interface FunctionThe Compiler produces this interface function only when the M-function uses the variable nargout
Supported Executable Types5-21Supported Executable TypesWrapper functions create a link between the Compiler-generated code and a supported executable
5 Controlling Code Generation5-22MEX-FilesThe -W mex -L C options produce the MEX-file wrapper, which includes the mexFunction interface that is stand
Supported Executable Types5-23The Compiler processes the string arguments passed to the main() function and sends them into the compiled M-function as
1Introducing theMATLAB CompilerThis chapter describes the MATLAB Compiler and its uses. It also includes new features, upgrading information, and lim
5 Controlling Code Generation5-24Simulink S-FunctionsThe -W simulink -L C options produce a Simulink S-function wrapper. Simulink S-function wrappers
Supported Executable Types5-25This example uses several functions from the toolbox\matlab\timefun directory (weekday, date, tic, calendar, toc) to cre
5 Controlling Code Generation5-26-B csharedlib:libtimefunwith-t -W lib:libtimefun -T link:lib -h libmmfile.mlibgiving the new statementmcc -t -W lib:l
Supported Executable Types5-27libtimefun.clibtimefun.hlibtimefun.exportslibtimefun.mliblibtimefun.extThe last file, libtimefun.ext, is the shared libr
5 Controlling Code Generation5-28Note On the mcc command line, you can access any mlib file by including the full path to the file. For example:mcc -
Supported Executable Types5-29This example uses several functions from the toolbox\matlab\timefun directory (weekday, date, tic, calendar, toc) to cre
5 Controlling Code Generation5-30The COM wrapper options create a superset of the files created when producing a C or C++ library wrapper. In addition
Supported Executable Types5-31When calling mbuild to link a library, the .dll file will be <component_name>_<major>_<minor>.dll. Thi
5 Controlling Code Generation5-32How mbuild Processes the File TypesThe mbuild option, -regsvr, uses the mwregsvr32 program to register the resulting
Supported Executable Types5-33C Signature.void mlxF(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]);mxArray *mlfNF( int nargout, mxArr
1 Introducing the MATLAB Compiler1-2IntroductionThis book describes Version 3.0 of the MATLAB® Compiler. The MATLAB Compiler takes M-files as input an
5 Controlling Code Generation5-34Porting Generated Code to a Different PlatformThe code generated by the MATLAB Compiler is portable among platforms.
Formatting Compiler-Generated Code5-35Formatting Compiler-Generated CodeThe formatting options allow you to control the look of the Compiler-generated
5 Controlling Code Generation5-36Default WidthNot specifying a page width formatting option uses the default of 80. Usingmcc -xg gasketgenerates this
Formatting Compiler-Generated Code5-37 NULL)); /* * * corners = [866 1;1 500;866 1000]; */ mclMline(15); mlfAssign(
5 Controlling Code Generation5-38Default IndentationNot specifying indent formatting options uses the default of four spaces for statements and two sp
Formatting Compiler-Generated Code5-39Modified IndentationThis example shows the same segment of code using a statement indentation of two and an expr
5 Controlling Code Generation5-40Including M-File Information in Compiler OutputThe annotation options allow you to control the type of annotation in
Including M-File Information in Compiler Output5-41Comments AnnotationTo include only comments from the source M-file in the generated output, usemcc
5 Controlling Code Generation5-42 mxDestroyArray(ans); mclSetCurrentLocalFunctionTable(save_local_function_table_); mclMlineExitFunction();}N
Including M-File Information in Compiler Output5-43Note When using the #line directive, the page-width directive is disabled in order to make the cod
Introduction1-3Note The phrase MATLAB interpreter refers to the application that accepts MATLAB commands, executes M-files and MEX-files, and behaves
5 Controlling Code Generation5-44Controlling Information in Run-Time ErrorsUse the debugline:setting option to include source filenames and line numbe
Including M-File Information in Compiler Output5-45your results are??? Error using ==> tmmultError using ==> *Inner matrix dimensions must agree
5 Controlling Code Generation5-46Interfacing M-Code to C/C++ CodeThe MATLAB Compiler supports calling arbitrary C/C++ functions from your M-code. You
Interfacing M-Code to C/C++ Code5-47private, or method). When using this pragma, the Compiler will generate an additional header file called file_exte
5 Controlling Code Generation5-48We recommend that you include this header file when defining the function. This function could be implemented in this
Interfacing M-Code to C/C++ Code5-49callback that references the specified function. Without this pragma, the -h option will not be able to locate and
5 Controlling Code Generation5-50
6Optimizing PerformanceThe MATLAB Compiler can perform various optimizations on your M-file source code that can make the performance of the generate
6 Optimizing Performance6-2Optimization BundlesAll optimizations are controlled separately, and you can enable or disable any of the optimizations. To
Optimization Bundles6-3• optimize_integer_for_loops• percolate_simple_types• speculateList All OptimizationsTo list all available optimizations, use-O
1 Introducing the MATLAB Compiler1-4New FeaturesMATLAB Compiler 3.0• The MATLAB Compiler now includes the MATLAB C/C++ Math and Graphics Libraries.Not
6 Optimizing Performance6-4Optimizing ArraysScalar Arrays(fold_scalar_mxarrays) When this optimization is enabled, all constant, scalar-valued array o
Optimizing Arrays6-5If you compile this with the -O none option, you get...mlfAssign(&y,mclMtimes(mlfDoubleMatrix(2, 2, _array0_, (double *)NULL),
6 Optimizing Performance6-6Optimizing LoopsSimple Indexing(array_indexing) This optimization improves the performance of simple one- and two-dimension
Optimizing Loops6-7Note This optimization causes the variable names in the resulting C program to differ from those in the M-file. Therefore, we reco
6 Optimizing Performance6-8for (; ; ) {...if (v_ == e_) {break;}++v_;}mlfAssign(&i, mlfScalar(v_));}...
Optimizing Conditionals6-9Optimizing Conditionals(optimize_conditionals) This optimization reduces the MATLAB conditional operators to scalar C condit
6 Optimizing Performance6-10Optimizing MATLAB ArraysScalars(percolate_simple_types) This optimization reduces the strength of operations on simple typ
7Reference
7 Reference7-2Functions — By CategoryPragmasCompiler FunctionsCommand Line Tools%#externalCall arbitrary C/C++ functions.%#functionfeval pragma.%#mexP
7-3Code Generation OptionsControl Compiler output.Optimization Options Improve the performance of the generated C/C++ code.Compiler and Environment Op
New Features1-5• The new option, -b, causes the Compiler to generate a Visual Basic (.bas) file that contains the Microsoft Excel Formula Function int
7 7-4Functions — By Name 7%#external . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5%#fun
%#external7-57%#externalPurpose Pragma to call arbitrary C/C++ functions from your M-codeSyntax %#externalDescription The %#external pragma informs th
%#function7-67%#functionPurpose feval pragmaSyntax %#function <function_name-list>Description This pragma informs the MATLAB Compiler that the s
%#mex7-77%#mexPurpose mex pragmaSyntax %#mexDescription This pragma informs the MATLAB Compiler to select the MEX-file over an existing M-file.If you
mbchar7-87mbcharPurpose Assert variable is a MATLAB character stringSyntax mbchar(x)Description The statementmbchar(x)causes the MATLAB Compiler to im
mbcharscalar7-97mbcharscalarPurpose Assert variable is a character scalarSyntax mbcharscalar(x)Description The statementmbcharscalar(x)causes the MATL
mbcharvector7-107mbcharvectorPurpose Assert variable is a character vector, i.e., a MATLAB stringSyntax mbcharvector(x)Description The statementmbchar
mbint7-117mbintPurpose Assert variable is integerSyntax mbint(n)Description The statementmbint(x)causes the MATLAB Compiler to impute that x is an int
mbint7-12x = 4 + 7.5i;Example This code in MATLAB causes mbint to generate an error message because n does not hold an integer value:n = 17.4;mbint(n)
mbintscalar7-137mbintscalarPurpose Assert variable is integer scalarSyntax mbintscalar(n)Description The statementmbintscalar(x)causes the MATLAB Comp
How to Contact The MathWorks:www.mathworks.com Webcomp.soft-sys.matlab [email protected] Technical [email protected] Product e
1 Introducing the MATLAB Compiler1-6Conditional Expressions. Reduces the MATLAB conditional operators to scalar C conditional operators when both oper
mbintvector7-147mbintvectorPurpose Assert variable is integer vectorSyntax mbintvector(n)Description The statementmbintvector(x)causes the MATLAB Comp
mbreal7-157mbrealPurpose Assert variable is realSyntax mbreal(n)Description The statementmbreal(x)causes the MATLAB Compiler to impute that x is real
mbrealscalar7-167mbrealscalarPurpose Assert variable is real scalarSyntax mbrealscalar(n)Description The statementmbrealscalar(x)causes the MATLAB Com
mbrealvector7-177mbrealvectorPurpose Assert variable is a real vectorSyntax mbrealvector(n)Description The statementmbrealvector(x)causes the MATLAB C
mbscalar7-187mbscalarPurpose Assert variable is scalarSyntax mbscalar(n)Description The statementmbscalar(x)causes the MATLAB Compiler to impute that
mbvector7-197mbvectorPurpose Assert variable is vectorSyntax mbvector(n)Description The statementmbvector(x)causes the MATLAB Compiler to impute that
mbuild7-207mbuildPurpose Compile and link source files that call functions in the MATLAB C/C++ Math Library or MATLAB C/C++ Graphics Library into a st
mbuild7-21-D<name>#<value>Define a symbol name and value to the C/C++ preprocessor. Equivalent to a #define <name> <value> di
mbuild7-22-lang <language>Specify compiler language. <language> can be c or cpp. By default, mbuild determines which compiler (C or C++) t
mbuild7-23-regsvr(Windows) Use the regsvr32 program to register the resulting shared library at the end of compilation. The Compiler uses this option
New Features1-7Faster C/C++ Math Library ApplicationsThe improved performance of the C/C++ Math Library is due in part to the added scalar accelerated
mbuild7-24Note Some of these options (-f, -g, and -v) are available on the mcc command line and are passed along to mbuild. Others can be passed alon
mcc7-257mccPurpose Invoke MATLAB CompilerSyntax mcc [-options] mfile1 [mfile2 ... mfileN][C/C++file1 ... C/C++fileN]Description mcc is the MATLAB comm
mcc7-26compilation, you can use one simple option, i.e., macro, that allows you to quickly accomplish basic compilation tasks. If you want to take adv
mcc7-27Table 7-3, -m Macro, shows the options that compose the -m macro and the information that they provide to the Compiler.Changing Macro Options.
mcc7-28command line options. Both the mccstartup file and the -B option are processed the same way.Note If you need to change the meaning of a macro
mcc7-29Conflicting Options on Command LineIf you use conflicting options, the Compiler resolves them from left to right, with the rightmost option tak
mcc7-30mcc -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
mcc7-31MATLAB Compiler Option FlagsThe MATLAB Compiler option flags perform various functions that affect the generated code and how the Compiler beha
mcc7-32In 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.
mcc7-33-S (Simulink S-Function). Produce a Simulink S-function that is compatible with the Simulink S-Function block. For example, to translate an M-f
1 Introducing the MATLAB Compiler1-8Running Compiler from DOS/UNIX ShellIf you run the Compiler from a DOS or UNIX shell, you are running from “outsid
mcc7-34-B sgl -t -W comhg:<component_name>,<class_name>,<version> -T link:lib -h libmmfile.mlib -i-B csglexcel (C Handle Graphics Ex
mcc7-35-B csharedlib (C Shared Library). Produce a C shared library. The -B csharedlib option is equivalent to the series of options-t -W lib:<shar
mcc7-36Table 7-5, Code/Comment Annotation Options, shows the available annotation options.To control the #line preprocessor directives that are includ
mcc7-37Table 7-7, Run-Time Error Annotation Options, shows the available debugline directive settings.For example, to include all of your M-code, incl
mcc7-38-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
mcc7-39-u (Number of Inputs). Provide more control over the number of valid inputs for your Simulink S-function. This option specifically sets the num
mcc7-40Caution When generating function wrappers, you must specify all M-files that are being linked together on the command line. These files are us
mcc7-41-y (Number of Outputs). Provide more control over the number of valid outputs for your Simulink S-function. This option specifically sets the n
mcc7-42Note You can use the -B option with a replacement expression as is at the DOS or UNIX prompt. To use -B with a replacement expression at the M
mcc7-43-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-alon
Uses of the Compiler1-9Uses of the CompilerThe MATLAB Compiler (mcc) can translate M-files into C files. The resultant C files can be used in any of t
mcc7-44The -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++ Mat
mcc7-45Table 7-11: Output Stage Options <Target> DescriptioncodegenTranslates M-files to C/C++ files and generates a wrapper file. The default
mcc7-46-v (Verbose). Display the steps in compilation, including• The Compiler version number• The source filenames as they are processed• The names o
mcc7-47-Y <license.dat File>. Use license information in license.dat file when checking out a Compiler license.mbuild/mex Options-f <filename
mcc7-48Note Multiple -M options do not accumulate; only the last -M option is used.-z <path> (Specifying Library Paths). Specify the path to us
mcc7-49mcc -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 -
mcc7-50
AMATLAB CompilerQuick ReferenceThis appendix summarizes the Compiler options and provides brief descriptions of how to perform common tasks.Common Us
A MATLAB Compiler Quick ReferenceA-2Common Uses of the CompilerThis section summarizes how to use the MATLAB Compiler to generate some of its more sta
Common Uses of the CompilerA-3Create a C++ Library. To create a C++ library, usemcc -p -W lib:libfoo -T compile:lib foo.mCreate a C Shared Library.To
1 Introducing the MATLAB Compiler1-10Figure 1-1: Developing MEX-FilesMATLAB users who do not have the MATLAB Compiler must write the source code for
A MATLAB Compiler Quick ReferenceA-4mccBold entries in the Comment/Options column indicate default values.Table A-1: mcc Quick Reference Option Descr
mccA-5F optionSpecifies format parametersoption = listexpression-indent:npage-width:nstatement-indent:ngGenerates debugging informationEquivalent to-
A MATLAB Compiler Quick ReferenceA-6O option:[on|off]O allO noneO listBuild an optimized executable.option =array_indexingfold_mxarraysfold_non_scalar
mccA-7w optionDisplays warning messagesoption = listlevellevel:stringwhere level = disableenableerrorNo w option displays only serious warnings (defa
A MATLAB Compiler Quick ReferenceA-8
BError and Warning MessagesThis appendix lists and describes error messages and warnings generated by the MATLAB Compiler. Compile-time messages are
B Error and Warning MessagesB-2Compile-Time ErrorsError: An error occurred while shelling out to mex/mbuild (error code = errorno). Unable to build ex
Compile-Time ErrorsB-3Error: File: filename Line: # Column: # () indexing must appear last in an index expression.If you use ordinary array indexing (
B Error and Warning MessagesB-4Error: File: filename Line: # Column: # An array for multiple LHS assignment cannot contain token.If the left-hand side
Compile-Time ErrorsB-5Error: File: filename Line: # Column: # Missing variable or function. An illegal name was used for a variable or function. For e
Uses of the Compiler1-11Creating Stand-Alone ApplicationsC Stand-Alone ApplicationsThe MATLAB Compiler, when invoked with the -m macro option, transla
B Error and Warning MessagesB-6Error: File: filename Line: # Column: # The PERSISTENT declaration must precede any use of the variable variablename.In
Compile-Time ErrorsB-7Error: Found illegal whitespace character in command line option: "string". The strings on the left and right side of
B Error and Warning MessagesB-8Error: The argument after the -option option must contain a colon. The format for this argument requires a colon. For m
Compile-Time ErrorsB-9Error: The specified file "filename" cannot be read. There is a problem with your specified file. For example, the fil
B Error and Warning MessagesB-10Error: Unknown annotation option: optionname. An invalid string was specified after the -A option. For a complete list
Warning MessagesB-11Warning MessagesThis section lists the warning messages that the MATLAB Compiler can generate. Using the -w option for mcc, you ca
B Error and Warning MessagesB-12Warning: File: filename Line: # Column: # The call to function "functionname" on this line could not be boun
Warning MessagesB-13will be called through an feval call. This is used so that the -h option will automatically compile the selected functions.Warning
B Error and Warning MessagesB-14Warning: File: filename Line: # Column: # The load statement cannot be translated unless it specifically lists the nam
Warning MessagesB-15Warning: File: filename Line: # Column: # The second output argument from the "functionname" function is only available
1 Introducing the MATLAB Compiler1-12Figure 1-2: Developing a Typical Stand-Alone C ApplicationM-File function to find the rank of a magic squaremcc
B Error and Warning MessagesB-16Warning: M-file "filename" was specified on the command line with full path of "pathname", but was
Warning MessagesB-17Warning: The name of a shared library should begin with the letters "lib". "libraryname" doesn’t.(missing_lib_
B Error and Warning MessagesB-18Run-Time ErrorsNote The error messages described in this section are generated by the Compiler into the code exactly
Run-Time ErrorsB-19Run-time Error: File: filename Line: # Column: # The function "functionname" was called with more than the declared numbe
B Error and Warning MessagesB-20
I-1IndexSymbols#line directives 5-42%#external 7-5%#function 7-6%#mex 7-7%#mex pragma 7-7.cshrc 4-11.DEF file 4-22A-A option flag 7-35add-inMATLAB for
IndexI-2C++compilerssupported on PCs 2-14supported on UNIX 2-4interfacing to M-code 5-46library wrapper 5-28required featurestemplates 4-6callback pro
IndexI-3dualitycommand/function 5-22Eembedded M-file 7-30environment variable 2-26library path 4-11out of environment space on Windows 2-25error messa
IndexI-4G-G option flag 7-47-g option flag 7-31gasket.m 3-2gcc compiler 2-4generated Compiler files 5-3generating P-code 7-35graphics applicationstrou
IndexI-5limitations of MATLAB Compiler 2.0 1-18built-in functions 1-18exist 1-18objects 1-18script M-file 1-18#line directives 5-42line numbers 7-38Li
Uses of the Compiler1-13See Chapter 4, “Stand-Alone Applications” for complete details regarding stand-alone applications.Figure 1-2, Developing a Typ
IndexI-6MATLAB librariesM-file Math 4-40, 7-44MATLAB plug-ins. See MEX wrapper.Matrix data type 1-17mbchar 7-8mbcharscalar 7-9mbcharvector 7-10mbint 7
IndexI-7M-filecompiling embedded 7-30examplegasket.m 3-2houdini.m 3-10main.m 4-36mrank.m 4-36, 4-42function 3-10MATLAB-provided 4-40script 1-18, 3-10M
IndexI-8options filecombining customized on PC 4-21locating 2-16locating on PC 4-16locating on UNIX 4-8making changes persist onPC 4-20UNIX 4-10modify
IndexI-9scalar arraysfolding 6-4script M-file 1-18, 3-10converting to function M-files 3-10setting default options 7-27S-function 3-7generating 3-7pas
IndexI-10templates requirement 4-6translate M to C-t option flag 7-44troubleshootingCompiler problems 2-27, 4-35mbuild problems 4-33MEX-file problems
1 Introducing the MATLAB Compiler1-14The MATLAB Compiler FamilyThis figure illustrates the various ways you can use the MATLAB Compiler. The shaded bl
The MATLAB Compiler Family1-15The Compiler takes your M-file(s) and can generate C or C++ code. It can also generate a wrapper file depending on your
iContentsPrefaceRelated Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xUsing this Guide . . . . . . . . .
1 Introducing the MATLAB Compiler1-16Why Compile M-Files?There are several reasons to compile M-files:• To create stand-alone applications• To create
Upgrading from Previous Versions of the Compiler1-17Upgrading from Previous Versions of the CompilerMATLAB Compiler 3.0 is fully compatible with previ
1 Introducing the MATLAB Compiler1-18Limitations and RestrictionsMATLAB CodeMATLAB Compiler 3.0 supports almost all of the functionality of MATLAB. Ho
Limitations and Restrictions1-19if (y==3)persistent xelsex = 3;endStand-Alone ApplicationsThe restrictions and limitations noted in the previous secti
1 Introducing the MATLAB Compiler1-20Fixing Callback Problems: Missing Functions When the Compiler creates a stand-alone application, it compiles the
Limitations and Restrictions1-21in callback strings.) The Compiler processes any function listed in a %#function pragma.For example, the call to the c
1 Introducing the MATLAB Compiler1-22
2Installation and ConfigurationThis chapter describes the system requirements for the MATLAB Compiler and installation and configuration information.
2 Installation and Configuration2-2System Configuration for MEX-FilesThis section outlines the steps necessary to configure your system to create MEX-
System Configuration for MEX-Files2-3Figure 2-1: MATLAB Compiler Installation Sequence for Creating MEX-FilesStartIs ANSI C or C++compiler installedF
ii ContentsCOM Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16Hiding Proprietary Algorithms . . . . . . . .
2 Installation and Configuration2-4UNIX WorkstationThis section examines the system requirements, installation procedures, and configuration procedure
UNIX Workstation2-5Note For a list of all the compilers supported by MATLAB, see the MathWorks Technical Support Department’s Technical Notes athttp:
2 Installation and Configuration2-6shows the preconfigured options files that are included with MATLAB for UNIX.Information on the options files is pr
UNIX Workstation2-7ANSI C or C++ CompilerTo install your ANSI C or C++ compiler, follow the vendor’s instructions that accompany your C or C++ compile
2 Installation and Configuration2-8If you do not need to change C or C++ compilers, or you do not need to modify your compiler options files, you can
UNIX Workstation2-9Note The setup option creates a user-specific, matlab directory in your individual home directory and copies the appropriate optio
2 Installation and Configuration2-10On UNIX, MEX-files are created with platform-specific extensions, as shown in Table 2-2, MEX-File Extensions for U
UNIX Workstation2-11MATLAB Compiler VerificationVerifying from MATLABOnce you have verified that you can generate MEX-files on your system, you are re
2 Installation and Configuration2-12Next, verify that invhilb is now a MEX-file by listing the invhilb files:ls invhilb.*You will see a list similar t
Microsoft Windows on PCs2-13Microsoft Windows on PCsThis section examines the system requirements, installation procedures, and configuration procedur
iii3Working with MEX-FilesA Simple Example — The Sierpinski Gasket . . . . . . . . . . . . . 3-2Compiling the M-File into a MEX-File . . . . . . .
2 Installation and Configuration2-14Supported ANSI C and C++ PC CompilersTo create C MEX-files, stand-alone C/C++ applications, or dynamically linked
Microsoft Windows on PCs2-15Known Compiler Limitations. There are several known restrictions regarding the use of supported compilers:• Some compilers
2 Installation and Configuration2-16Compiler Options FilesThe MathWorks provides options files for every supported C or C++ compiler. These files cont
Microsoft Windows on PCs2-17created during the -setup process, in a subdirectory of your user profile directory, named Application Data\MathWorks\MATL
2 Installation and Configuration2-18Things to Be Aware ofThis table provides information regarding the installation and configuration of a C/C++ compi
Microsoft Windows on PCs2-19mex VerificationChoosing a CompilerSystems with Exactly One C/C++ Compiler. If you have properly installed the MATLAB Comp
2 Installation and Configuration2-20[1] : Borland compiler in T:\Borland\BC.500[2] : WATCOM compiler in T:\watcom\c.106[0] : NonePlease select a compi
Microsoft Windows on PCs2-21[15] Microsoft Visual C/C++ version 5.0[16] WATCOM C/C++ version 11[17] WATCOM C/C++ version 10.6[0] NoneCompiler: 14Your
2 Installation and Configuration2-22The setup option copies the appropriate options file to your user profile directory. To make your user-specific ch
Microsoft Windows on PCs2-23mex -setup and select Microsoft Visual C/C++ version 5 or 6. For more information about the add-in, see “Using an Integrat
iv ContentsBuilding Stand-Alone Applications on PCs . . . . . . . . . . . . . 4-15Configuring for C or C++ . . . . . . . . . . . . . . . . . . . .
2 Installation and Configuration2-24Note Before you test to see if the Compiler can generate MEX-files from the DOS command prompt, you may want to d
Troubleshooting2-25TroubleshootingThis section identifies some of the more common problems that can occur when installing and configuring the MATLAB C
2 Installation and Configuration2-26mex Works from Shell But Not from MATLAB (UNIX). If the commandmex -x yprime.cworks from the UNIX shell prompt but
Troubleshooting2-27Troubleshooting the CompilerOne problem that might occur when you try to use the Compiler involves licensing.Licensing Problem. If
2 Installation and Configuration2-28
3Workingwith MEX-FilesThis chapter gets you started compiling M-files with the MATLAB Compiler.A Simple Example — The Sierpinski Gasket (p. 3-2)Creat
3 Working with MEX-Files3-2A Simple Example — The Sierpinski GasketConsider an M-file function called gasket.m:function theImage = gasket(numPoints)%G
A Simple Example — The Sierpinski Gasket3-3The curve can be graphed in many ways. Sierpinski's method is• Start with a triangle and from it remov
3 Working with MEX-Files3-4This example uses the -x macro option to create the MEX-file. For more information on this Compiler option, see the mcc ref
A Simple Example — The Sierpinski Gasket3-5Figure 3-1, The Sierpinski Gasket for 50,000 Points, shows the results.Figure 3-1: The Sierpinski Gasket f
v5Controlling Code GenerationCode Generation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2Example M-Files . . . . . . . .
3 Working with MEX-Files3-6Compiler Options and MacrosThe MATLAB Compiler uses a family of options, also called option flags, to control the functiona
Generating Simulink S-Functions3-7Generating Simulink S-FunctionsYou can use the MATLAB Compiler to generate Simulink C MEX S-functions. This allows y
3 Working with MEX-Files3-8The result is an S-function described in the following files:mfilename.cmfilename.hmfilename_simulink.cmfilename.ext(where
Generating Simulink S-Functions3-9Data TypeThe input and output vectors for the Simulink S-function must be double-precision vectors or scalars. You m
3 Working with MEX-Files3-10Converting Script M-Files to Function M-FilesMATLAB provides two ways to package sequences of MATLAB commands:• Function M
Converting Script M-Files to Function M-Files3-11in the MATLAB workspace. If it is important to have m accessible from the MATLAB workspace, you can c
3 Working with MEX-Files3-12
4 Stand-AloneApplicationsThis chapter explains how to use the MATLAB Compiler to code and build stand-alone applications.Stand-alone applications run
4 Stand-Alone Applications4-2Differences Between MEX-Files and Stand-Alone ApplicationsMEX-files and stand-alone applications differ in these respects
Differences Between MEX-Files and Stand-Alone Applications4-3For more information about distributing a C application, see “Distributing Stand-Alone Ap
vi ContentsInterfacing M-Code to C/C++ Code . . . . . . . . . . . . . . . . . . . . . 5-46C Example . . . . . . . . . . . . . . . . . . . . . . .
4 Stand-Alone Applications4-4Building Stand-Alone C/C++ ApplicationsThis section explains how to build stand-alone C and C++ applications on UNIX syst
Building Stand-Alone C/C++ Applications4-5Figure 4-1, Sequence for Creating Stand-Alone C/C++ Applications, shows the sequence on both platforms. The
4 Stand-Alone Applications4-6Getting StartedIntroducing mbuildThe MathWorks utility, mbuild, lets you customize the configuration and build process. T
Building Stand-Alone Applications on UNIX4-7Building Stand-Alone Applications on UNIXThis section explains how to compile and link C or C++ source cod
4 Stand-Alone Applications4-8Locating Options Filesmbuild locates your options file by searching the following:• The current directory•$HOME/.matlab/R
Building Stand-Alone Applications on UNIX4-9The setup option creates a user-specific options file for your ANSI C or C++ compiler. Executing mbuild -s
4 Stand-Alone Applications4-10the current linker settings, or you want to disable a particular set of warnings, you should use the setup option.If you
Building Stand-Alone Applications on UNIX4-11Verifying mbuildThere is C source code for an example ex1.c included in the <matlab>/extern/example
4 Stand-Alone Applications4-12be found and loaded. Consequently, to share a Compiler-generated, stand-alone application with another user, you must pr
Building Stand-Alone Applications on UNIX4-13About the mbuild ScriptThe mbuild script supports various options that allow you to customize the buildin
viiAMATLAB Compiler Quick ReferenceCommon Uses of the Compiler . . . . . . . . . . . . . . . . . . . . . . . . . A-2mcc . . . . . . . . . . . . .
4 Stand-Alone Applications4-14Note If you distribute an application created with the math libraries on Digital UNIX, your users must have both the C+
Building Stand-Alone Applications on PCs4-15Building Stand-Alone Applications on PCsThis section explains how to compile and link the C/C++ code gener
4 Stand-Alone Applications4-16Note You can override the language choice that is determined from the extension by using the -lang option of mbuild. Fo
Building Stand-Alone Applications on PCs4-17Choosing a CompilerSystems with Exactly One C/C++ Compiler. If the MATLAB Compiler and your supported C or
4 Stand-Alone Applications4-18[1] Lcc C version 2.4 in D:Applications\Mathworks\sys\lcc [2] Microsoft Visual C/C++ version 6.0 in D:\Applications\Micr
Building Stand-Alone Applications on PCs4-19Compiler: 10Your machine has a Microsoft Visual C/C++ compiler located atD:\Applications\Microsoft Visual
4 Stand-Alone Applications4-20specific needs and save the modified file. This sets your default compiler’s options file to your specific version. Tabl
Building Stand-Alone Applications on PCs4-21Combining Customized C and C++ Options Files. The options files for mbuild have changed as of MATLAB 5.3 (
4 Stand-Alone Applications4-22Verifying mbuildThere is C source code for an example, ex1.c, included in the <matlab>\extern\examples\cmath direc
Building Stand-Alone Applications on PCs4-23 1.0000 + 7.0000i 4.0000 +10.0000i 2.0000 + 8.0000i 5.0000 +11.0000i 3.0000 + 9.0000i 6.0000 +
Commentaires sur ces manuels