The building process includes generating code in C or C++ from a model and building an executable program from the generated code. This example can use a generic real-time (GRT) or an embedded real-time (ERT) system target file (STF) for code generation. The resulting standalone program runs on your development computer, independent of external timing and events.
This example uses a local copy of the
model, stored in its own folder,
aircraftexample. Set up your
working folder as follows:
In the MATLAB® Current Folder browser, navigate to a folder to which you have write access.
To create the working folder, enter the following MATLAB command:
aircraftexample your working
The model appears in the Simulink® Editor model window.
In the model window, choose File > Save As. Navigate to your working folder,
aircraftexample. Save a copy of the
slexAircraftExample model as
While producing code, the code generator creates a build
folder within your working folder. The build folder name is
derived from the name of the source model and the chosen system target file. The
build folder stores generated source code and other files created during the build
process. Examine the build folder contents at the end of this example.
When a model contains Model blocks (references to other models), the model build
creates special subfolders in your Code generation folder to organize
code for the referenced models. These code generation folders exist alongside
product build folders and are named
slprj. For more information,
see Generate Code for Model Reference Hierarchy.
slprj folder, a subfolder named
_sharedutils contains generated code that can be shared
To generate code from your model, you must change some of the model configuration parameters. In particular, the generic real-time (GRT) system target file and most other system target files require that the model specifies a fixed-step solver.
For models that specify variable-step solvers, the code generator produces
code only if the models also specify rapid simulation (
or S-function system target files.
myAircraftExample model if
it is not already open.
In the Configuration Parameters dialog box, specify configuration parameter values for the solver:
Fixed step size (fundamental sample time):
Treat each discrete rate as a separate task:
Save the model.
In these examples and in most applications, you do not need to specify these parameters individually. The examples use the ready-to-run generic real-time target (GRT) configuration. The GRT system target file builds a standalone executable program that runs on your desktop computer.
To select the GRT system target file:
myAircraftExample model if it is not already
In the Configuration Parameters dialog box, in the
System target file field, enter
grt.tlc. Then click Apply.
You see selections for Toolchain
Automatically locate an installed
toolchain), and Build Configuration
Save the model.
If you click Browse, a System Target File Browser opens
and displays the system target files on the MATLAB path. Some system target files require additional products. For
ert.tlc requires Embedded Coder®.
myAircraftExample model if it is not already
In the Configuration Parameters dialog box, specify settings:
Retain .rtw file
Start TLC debugger when generating
Start TLC coverage when generating
Enable TLC assertion
Use the default settings.
Select Code Generation > Interface.
From the Shared code
placement list, select
Shared location. The
build process places generated code for utilities in
a subfolder within your Code generation folder.
Under the Advanced parameters, clear the Classic call interface check box.
Under the Advanced parameters, select the Single output/update function check box.
In Code Generation > Report, select Create code generation
report and Open report
automatically. This action enables the software
to create and display a code generation report for the
Click Apply and save the model.
The build process generates C code from the model. It then compiles and links the generated program to create an executable image. To build and run the program:
open, perform one of these actions:
### Starting build procedure for model: myAircraftExample
The contents of many of the succeeding messages depends on your compiler and operating system. The final messages include:
### Created executable myAircraftExample.exe ### Successful completion of build procedure for model: myAircraftExample ### Creating HTML report file myAircraftExample_codegen_rpt.html
The code generation folder now contains an executable,
Windows® platforms) or
(UNIX® platforms). In addition, the build process has created an
slprj folder and a
myAircraftExample_grt_rtw folder in your Code generation folder.
After generating the code for the
model, the build process displays a code generation report. See Report Generation for more information about how to create
and use a code generation report.
To see the contents of the working folder after the build, enter the
>> dir . myAircraftExample.slx slprj .. myAircraftExample.slx.autosave myAircraftExample.exe myAircraftExample_grt_rtw
To run the executable from the Command Window, type
character passes the command that follows it to the operating system, which
runs the standalone
>> !myAircraftExample ** starting the model ** ** created myAircraftExample.mat **
To see the files created in the build folder, use the
ls command again. The exact
list of files produced varies among MATLAB platforms and versions. Here is a sample list from a
>> dir myAircraftExample_grt_rtw . rt_main.obj myAircraftExample_data.c .. rtmodel.h myAircraftExample_data.obj buildInfo.mat rtw_proj.tmw myAircraftExample_private.h codeInfo.mat myAircraftExample.bat myAircraftExample_ref.rsp defines.txt myAircraftExample.c myAircraftExample_types.h html myAircraftExample.h modelsources.txt myAircraftExample.mk rt_logging.obj myAircraftExample.obj
The build process creates a build folder and names it
is the name of the source
is the system target
file selected for the model. In this example, the build folder is named
The build folder includes the following generated files.
Standalone C code that implements the model
An include header file containing definitions of parameters and state variables
Header file containing common include definitions
Forward declarations of data types used in the code
Master header file for including generated code in the static
main program (its name does not change, and it simply includes
The code generation report that you created for the
myAircraftExample model displays a link for each of these
files. You can click the link explore the file contents.
The build folder contains other files used in the build process. They include:
myAircraftExample.mk — Makefile for building
executable using the specified Toolchain.
myAircraftExample.bat — Batch control
rtw_proj.tmw — Marker file
buildInfo.mat — Build information for relocating
generated code to another development environment
defines.txt — Preprocessor definitions required
for compiling the generated code
myAircraftExample_ref.rsp — Data to include as
command-line arguments to
mex (Windows systems
The build folder also contains a subfolder,
contains the files that make up the code generation report. For more information,
see Reports for Code Generation.
After producing code, the code generator produces a customized makefile,
.mk. The generated makefile
make system utility to compile and link source code
generated from the model, any required harness
program, libraries, or user-provided modules. The code generator produces the file
.mk regardless of the
approach that you use for build process control:
If you use the toolchain approach, the code generator creates
.mk based on the
model Toolchain settings. You can modify generation
of the makefile through the
If you use the template makefile approach, the code generator creates
.mk from a system
system stands for the selected system
target file name). The system template makefile is designed for your
system target file. You can modify the template makefile to specify
compilers, compiler options, and additional information for the creation
of the executable.
For more information, see Choose Build Approach and Configure Build Process.