Main Content

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:

  1. In the MATLAB® Current Folder browser, navigate to a folder to which you have write access.

  2. To create the working folder, enter the following MATLAB command:

    mkdir aircraftexample
  3. Make aircraftexample your working folder:

    cd aircraftexample
  4. Open the slexAircraftExample model:

    slexAircraftExample

    The model appears in the Simulink® Editor model window.

  5. In the model window, choose File > Save As. Navigate to your working folder, aircraftexample. Save a copy of the slexAircraftExample model as myAircraftExample.

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 model_target_rtw, 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.

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.

  1. Open the myAircraftExample model if it is not already open.

  2. In the Configuration Parameters dialog box, specify configuration parameter values for the solver:

    • Start time: 0.0

    • Stop time: 60

    • Type: Fixed-step

    • Solver: ode5 (Dormand-Prince)

    • Fixed step size (fundamental sample time): 0.1

    • Treat each discrete rate as a separate task: Off

  3. Click Apply.

  4. 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:

  1. Open the myAircraftExample model if it is not already open.

  2. 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).

  3. 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

  1. Open the myAircraftExample model if it is not already open.

  2. In the Configuration Parameters dialog box, specify settings:

    1. These Code Generation advanced parameters 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)

      Use the default settings.

    2. Use the default Code Generation > Comments settings.

    3. The Code Generation > Identifiers options control the look and feel of generated code. Use the default settings.

    4. Select Code Generation > Interface.

      1. 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.

      2. Under the Advanced parameters, clear the Classic call interface check box.

      3. Under the Advanced parameters, select the Single output/update function check box.

    5. 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 myAircraftExample model.

  3. 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:

  1. With the myAircraftExample model 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 rtwbuild command from the MATLAB command line.

    • Run the slbuild command 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 myAircraftExample_codegen_rpt.html

    The code generation folder now contains an executable, myAircraftExample.exe (Microsoft® Windows® platforms) or myAircraftExample (UNIX® platforms). In addition, the build process has created an slprj folder and a myAircraftExample_grt_rtw folder in your Code generation folder.

    Note

    After generating the code for the myAircraftExample 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.

  2. To see the contents of the working folder after the build, enter the dir or ls command:

    >> dir
    
    .                               myAircraftExample.slx           slprj
    ..                              myAircraftExample.slx.autosave
    myAircraftExample.exe           myAircraftExample_grt_rtw
  3. 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 standalone myAircraftExample program.

    >> !myAircraftExample
    
    ** starting the model **
    ** created myAircraftExample.mat **
  4. To see the files created in the build folder, use the dir or ls command 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
    defines.txt           myAircraftExample.c          myAircraftExample_types.h
    html                  myAircraftExample.h
    modelsources.txt      myAircraftExample.mk
    rt_logging.obj        myAircraftExample.obj

Contents of the Build Folder

The build process creates a build folder and names it model_target_rtw, where model is the name of the source model and target is the system target file selected for the model. In this example, the build folder is named myAircraftExample_grt_rtw.

The build folder includes the following generated files.

FileDescription

myAircraftExample.c

Standalone C code that implements the model

myAircraftExample.h

An include header file containing definitions of parameters and state variables

myAircraftExample_private.h

Header file containing common include definitions

myAircraftExample_types.h

Forward declarations of data types used in the code

rtmodel.h

Master header file for including generated code in the static main program (its name does not change, and it simply includes myAircraftExample.h)

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) files

  • myAircraftExample.bat — Batch control file

  • 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 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.mk based on the model Toolchain settings. You can modify generation of the makefile through the rtwmakecfg.m API.

  • If you use the template makefile approach, the code generator creates model.mk from a system template file, system.tmf (where 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.

Related Topics