Build Process Workflow for Real-Time Systems
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.
Working Folder
This example uses a local copy of the slexAircraftExample
                model, stored in its own folder, aircraftexample. Set up your
                    working folder as follows:
In the MATLAB® Files panel, navigate to a folder to which you have write access.
To create the working folder, enter the following MATLAB command:
mkdir aircraftexampleMake
aircraftexampleyour working folder:cd aircraftexampleOpen the
slexAircraftExamplemodel:openExample('slexAircraftExample')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 theslexAircraftExamplemodel asmyAircraftExample.
Build Folder and Code Generation Folders
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.model_target_rtw
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.
Under the slprj folder, a subfolder named
                    _sharedutils contains generated code that can be shared
                between models.
Set Model Parameters for Code Generation
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.
Note
For models that specify variable-step solvers, the code generator produces
                    code only if the models also specify rapid simulation (rsim)
                    or S-function system target files.
Open the
myAircraftExamplemodel if it is not already open.In the Configuration Parameters dialog box, specify configuration parameter values for the solver:
Start time:
0.0Stop time:
60Type:
Fixed-stepSolver:
ode5 (Dormand-Prince)Fixed step size (fundamental sample time):
0.1Treat each discrete rate as a separate task:
Off
Click Apply.
Save the model.
Configure Build Process
To configure the build process for your model, choose a system target file, a
                toolchain or template makefile, and a make command.
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:
Open the
myAircraftExamplemodel if it is not already open.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 (Faster Builds).
Save the model.
Note
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
                    example, ert.tlc requires Embedded Coder®.
Set Code Generation Parameters
Open the
myAircraftExamplemodel if it is not already open.In the Configuration Parameters dialog box, specify settings:
Use the default settings for the Code Generation advanced parameters, which control build verbosity and debugging:
Verbose build (
RTWVerbose)Retain .rtw file (
RetainRTWFile)Profile TLC (
ProfileTLC)Start TLC debugger when generating code (
TLCDebug)Start TLC coverage when generating code (
TLCCoverage)Enable TLC assertion (
TLCAssert)
The Code Generation > Identifiers options control the look and feel of generated code. 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, 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
myAircraftExamplemodel.
Click Apply and save the model.
Build and Run a Program
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:
With the
myAircraftExamplemodel open, perform one of these actions:On the Apps tab, open the Simulink Coder app. In the C Code tab, click Build.
Press Ctrl+B.
Run the
slbuildcommand from the MATLAB command line.
You see code generation and compilation messages in the Command Window. The initial message is:
### 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 index.html
The code generation folder now contains an executable,
myAircraftExample.exe(Microsoft® Windows® platforms) ormyAircraftExample(UNIX® platforms). In addition, the build process has created anslprjfolder and amyAircraftExample_grt_rtwfolder in your Code generation folder.Note
After generating the code for the
myAircraftExamplemodel, 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
dirorlscommand:>> dir . myAircraftExample.slx slprj .. myAircraftExample.slx.autosave myAircraftExample.exe myAircraftExample_grt_rtw
To run the executable from the Command Window, type
!myAircraftExample. The!character passes the command that follows it to the operating system, which runs the standalonemyAircraftExampleprogram.>> !myAircraftExample ** starting the model ** ** created myAircraftExample.mat **
To see the files created in the build folder, use the
dirorlscommand again. The exact list of files produced varies among MATLAB platforms and versions. Here is a sample list from a Windows platform:>> 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 myAircraftExample.c myAircraftExample_types.h html myAircraftExample.h myAircraftExample.mk rt_logging.obj myAircraftExample.obj
Contents of the Build Folder
The build process creates a build folder and names it
                        ,
                where model_target_rtw is the name of the source
                model and model is the system target
                file selected for the model. In this example, the build folder is named
                    targetmyAircraftExample_grt_rtw.
The build folder includes the following generated files.
| File | Description | 
|---|---|
  | 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  | 
  | 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.Object (
.obj) filesmyAircraftExample.bat— Batch control filertw_proj.tmw— Marker filebuildInfo.mat— Build information for relocating generated code to another development environmentmyAircraftExample_ref.rsp— Data to include as command-line arguments tomex(Windows systems only)
The build folder also contains a subfolder, html, which
                contains the files that make up the code generation report. For more information,
                see Reports for Code Generation.
Customized Makefile Generation
After producing code, the code generator produces a customized makefile,
                    model.mk. The generated makefile
                instructs the 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
                    model.mk regardless of the
                approach that you use for build process control:
If you use the toolchain approach, the code generator creates
model.mkbased on the model Toolchain settings. You can modify generation of the makefile through thertwmakecfg.mAPI.If you use the template makefile approach, the code generator creates
model.mkfrom a system template file,system.tmf(wheresystemstands 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 Configure Toolchain (ToolchainInfo) or Template Makefile Build Process.