rtwbuild

Build generated code from a model

Description

example

rtwbuild(model) generates code from model based on current model configuration parameter settings. If model is not already loaded into the MATLAB® environment, rtwbuild loads it before generating code.

If you clear the Generate code only model configuration parameter, the function generates code and builds an executable image.

To reduce code generation time, when rebuilding a model, rtwbuild provides incremental model build. The code generator rebuilds a model or submodels only when they have changed since the most recent model build. To force a top-model build, see the 'ForceTopModelBuild' argument.

example

rtwbuild(model,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

example

rtwbuild(subsystem) generates code from subsystem based on current model configuration parameter settings. Before initiating the build, open (or load) the parent model.

If you clear the Generate code only model configuration parameter, the function generates code and builds an executable image.

example

rtwbuild(subsystem,'Mode','ExportFunctionCalls') generates code from subsystem that includes function calls that you can export to external application code if you have Embedded Coder®.

example

blockHandle = rtwbuild(subsystem,'Mode','ExportFunctionCalls') returns the handle to a SIL block created for code generated from the specified subsystem if the Create block configuration parameter is set to SIL and if you have Embedded Coder. You can then use the SIL block for numerical equivalence testing.

Examples

collapse all

Generate C code for model rtwdemo_rtwintro.

rtwbuild('rtwdemo_rtwintro')

For the GRT system target file, the code generator produces the following code files and places them in folders rtwdemo_rtwintro_grt_rtw and slprj/grt/_sharedutils.

Model FilesShared FilesInterface FilesOther Files

rtwdemo_rtwintro.c

rtwdemo_rtwintro.h

rtwdemo_rtwintro_private.h

rtwdemo_rtwintrotypes.h

rtGetInf.c

rtGetInf.h

rtGetNaN.c

rtGetNaN.h

rt_nonfinite.c

rt_nonfinite.h

rtwtypes.h

multiword_types.h

builtin_typeid_types.h

rtmodel.h

rt_logging.c

If the following model configuration parameters settings apply, the code generator produces additional results.

Parameter SettingResults
Code Generation > Generate code only is clearedExecutable image rtwdemo_rtwintro.exe
Code Generation > Report > Create code generation report is selectedReport that provides information and links to generated code files, subsystem and code interface reports, entry-point functions, inports, outports, interface parameters, and data stores

Generate code and build an executable image for rtwdemo_mdlreftop, which refers to model rtwdemo_mdlrefbot, regardless of model checksums and parameter settings.

rtwbuild('rtwdemo_mdlreftop', ...
    'ForceTopModelBuild',true)

Introduce an error to model rtwdemo_mdlreftop and save the model as rtwdemo_mdlreftop_witherr. Display build error messages in the Diagnostic Viewer and in the Command Window while generating code and building an executable image for model rtwdemo_mdlreftop_witherr.

rtwbuild('rtwdemo_mdlreftop_witherr', ...
    'OkayToPushNags',true)

Generate C code for subsystem Amplifier in model rtwdemo_rtwintro.

rtwbuild('rtwdemo_rtwintro/Amplifier')

For the GRT target, the code generator produces the following code files and places them in folders Amplifier_grt_rtw and slprj/grt/_sharedutils.

Model FilesShared FilesInterface FilesOther Files

Amplifier.c

Amplifier.h

Amplifier_private.h

Amplifier_types.h

rtGetInf.c

rtGetInf.h

rtGetNaN.c

rtGetNaN.h

rt_nonfinite.c

rt_nonfinite.h

rtwtypes.h

multiword_types.h

builtin_typeid_types.h

rtmodel.h

rt_logging.c

If you apply the parameter settings listed in the table, the code generator produces the results listed.

Parameter SettingResults
Code Generation > Generate code only is clearedExecutable image Amplifier.exe
Code Generation > Report > Create code generation report is selectedReport that provides information and links to generated code files, subsystem and code interface reports, entry-point functions, inports, outports, interface parameters, and data stores

To export the image to external application code, build an executable image from a function-call subsystem.

rtwdemo_exporting_functions
rtwbuild('rtwdemo_exporting_functions/rtwdemo_subsystem','Mode','ExportFunctionCalls')

The executable image rtwdemo_subsystem.exe appears in your working folder.

From a function-call subsystem, create a SIL block that you can use to test the code generated from a model.

Open subsystem rtwdemo_subsystem in model rtwdemo_exporting_functions and set the Create block model configuration parameter to SIL.

Create the SIL block.

mysilblockhandle = rtwbuild('rtwdemo_exporting_functions/rtwdemo_subsystem',...
'Mode','ExportFunctionCalls')

The code generator produces a SIL block for the generated subsystem code. You can add the block to an environment or test harness model that supplies test vectors or stimulus input. You can then run simulations that perform SIL tests and verify that the generated code in the SIL block produces the same result as the original subsystem.

Name the initialization function generated when building an executable image from a function-call subsystem.

rtwdemo_exporting_functions
rtwbuild('rtwdemo_exporting_functions/rtwdemo_subsystem',...
'Mode','ExportFunctionCalls','ExportFunctionInitializeFunctionName','subsysinit')

The initialization function name subsysinit appears in rtwdemo_subsystem_ert_rtw/ert_main.c.

Display build information in the Build Status window while generating code and running a parallel build of model rtwdemo_mdlreftop_witherr.

rtwbuild('rtwdemo_mdlreftop_witherr', ...
    'OpenBuildStatusAutomatically',true)

Input Arguments

collapse all

Model for which to generate code or build an executable image, specified as an object or a character vector representing the model name.

Example: 'rtwdemo_exporting_functions'

Subsystem for which to generate code or build an executable image, specified as a character vector representing the subsystem name or the full block path.

Example: 'rtwdemo_exporting_functions/rtwdemo_subsystem'

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: rtwbuild('rtwdemo_mdlreftop','ForceTopModelBuild',true)Example: rtwbuild(subsystem,'Mode','ExportFunctionCalls','ExportFunctionInitializeFunctionName', fcnname)

Force regeneration of top model code, specified as true or false.

ActionSpecify
Force the code generator to regenerate code for the top model of a system that includes referenced models true
Specify that the code generator determine whether to regenerate top model code based on model and model parameter changes false

Consider forcing regeneration of code for a top model if you change items associated with external or custom code, such as code for a custom target. For example, set ForceTopModelBuild to true if you change:

  • TLC code

  • S-function source code, including rtwmakecfg.m files

  • Integrated custom code

Alternatively, you can force regeneration of top model code by deleting folders in the code generation folder (Simulink), such as slprj or the generated model code folder.

Display error messages from the build in Diagnostic Viewer, specified as true or false.

ActionSpecify
Display build error messages in the Diagnostic Viewer and in the Command Windowtrue
Display build error messages in the Command Window onlyfalse

If you do not specify a value, the Generate code only (GenCodeOnly) option on the Code Generation pane controls build process behavior.

If you specify a value, the argument overrides the Generate code only (GenCodeOnly) option on the Code Generation pane.

ActionSpecify

Generate code only.

true

Generate code and build executable file.

false

If you have Embedded Coder, generates code from subsystem that includes function calls that you can export to external application code.

Name the exported initialization function for specified subsystem.

Display build information in the Build Status window, specified as true or false. For more information about using the Build Status window, see Monitor Parallel Building of Referenced Models.

The Build Status window supports parallel builds of referenced model hierarchies. Do not use the Build Status window for serial builds.

ActionSpecify
Display build information in the Build Status windowtrue
No actionfalse

Specify whether to generate obfuscated C code, specified as true or false.

ActionSpecify
Generate obfuscated C code that you can share with third parties with reduced likelihood of compromising intellectual property.true
No action.false

Output Arguments

collapse all

Handle to SIL block created for generated subsystem code. Returned only if both of the following conditions apply:

  • You are licensed to use Embedded Coder software.

  • Create block model configuration parameter is set to SIL.

Tips

You can initiate code generation and the build process by:

  • Pressing Ctrl+B.

  • Selecting Code > C/C++ Code > Build Model.

  • Invoking the slbuild command from the MATLAB command line.

Extended Capabilities

Introduced in R2009a