メインコンテンツ

Build Application Code

R2026b

Multiple approaches are available for building service-oriented architecture application code. The approach you choose depends on whether you are developing an application or a system architecture consisting of one or more applications. The approach also depends on available or preferred tooling approaches.

Choose an Approach to Build Application Code

To build your application code, choose one of these approaches:

Build ApproachReasons to Choose
Generate source code only
  • Early in software component development process, you want to:

    • Evaluate whether the code generator produces source code.

    • Review and evaluate the generated component source code.

    • Perform static analysis on the component source code.

  • Later in software component development process, you want to:

    • Integrate component source code into a wider system manually.

    • Check generated source code into a source control system, such as Git™.

  • You need to generate component source code on a platform that is different from the platform where the code is compiled.

Build generated source code outside of the MATLAB® environment (algorithm export)

Build and integrate generated component source code outside of the MATLAB environment for conducting component verification.

Build application as a top model and standalone unitBuild an executable program for the component as a top model for conducting component verification.
Build application for software-in-the-loop (SIL) simulationTest for numerical equivalence between normal mode simulation results and simulation of generated component code.
Build application in the context of a systemGenerate and build component code after integrating the components into an architecture model representing a system.
Build application by using a Docker containerProvide an environment that is preconfigured with the toolchain, libraries, and platform dependencies required to compile and run the application onLinux® . Monitor and tune application signals and parameters if configured to use external mode or a Universal Measurement and Calibration Protocol (XCP) server.

Application Build Configuration and Startup

Use this table as a guide for configuring and starting a build depending on the build approach that you choose.

Build ApproachConfiguration OptionsBuild Environment and Tooling OptionsRun-Time Environment OptionsOther Notes
Generate source code only
  • Set Hardware board to align with your target computer.

  • Set Toolchain to one of these values:

    Automatically locate an installed toolchain
    GNU gcc/g++ | gmake (64-bit Linux)
    GNU gcc/g++ | CMake/gmake (64-bit Linux)
    GNU gcc/g++ | CMake/Ninja (64-bit Linux)
    CMake

On the development computer:

  • On the Software Component Designer toolstrip, select Generate Code.

  • Use the slbuild function.

Use the gmake or CMake command.

  • Development computer

  • Target computer

  • If model configuration parameter Hardware board is set to None and Toolchain is set to a CMake value, start a build by using the slbuild function or the Run on Custom Hardware app.

  • To start a build by using the gmake command, open a terminal window and navigate to the folder that contains your source code and the makefile. Then, run the gmake command. Verify that the executable program file is in the build folder.

  • To start a build by using the CMake command, open a terminal window and navigate to your project root folder, which contains an exe folder. To separate generated build files and artifacts from the source code, in the exe folder, create a build folder. From the build folder, run CMake. Verify that the executable program file is in the build folder.

Build generated source code outside of the MATLAB environment (algorithm export)
  • Set Hardware board to align with your target computer.

  • Set Toolchain to one of these values:

    CMake
    GNU gcc/g++ | CMake/gmake (64-bit Linux)
    GNU gcc/g++ | CMake/Ninja (64-bit Linux)

On the development computer:

  • On the Software Component Designer toolstrip, from the Generate Code menu, select Generate Code.

  • Programmatically or at the MATLAB command line, use the slbuild function.

Use the CMake command.

  • Development computer

  • Target computer

  • You need to compile application code on a platform that is different from the platform on which the code was generated.

  • Build the generated application as part of a larger system that is being developed outside the MATLAB environment.

Build application as a top model and standalone unit
  • Set Hardware board to None.

  • Set Toolchain to one of these values:

    Automatically locate an installed toolchain
    GNU gcc/g++ | gmake (64-bit Linux) GNU gcc/g++ | CMake/gmake (64-bit Linux)
    GNU gcc/g++ | CMake/Ninja (64-bit Linux)

On the development computer:

  • On the Software Component Designer toolstrip, from the Generate Code menu, select Build.

  • Programmatically or at the MATLAB command line, use the slbuild function.

  • Use the Run on Custom Hardware app to build an executable program for running external mode simulations during which you can tune parameters in real time and monitor target application signals.

  • On development computer, Linux Runtime Manager. (See the Build Approach "Build application by using a Docker container" in this table.)

Use the CMake command.

  • Development computer

  • Target computer

  • If model configuration parameter Hardware board is set to None and Toolchain is set to a CMake value, start a build by using the slbuild function or the Run on Custom Hardware app.

  • To start a build by using the CMake command, open a terminal window and navigate to your project root folder, which contains an exe folder. To separate generated build files and artifacts from the source code, in the exe folder, create a build folder. From in the build folder, run CMake. Verify that the executable program file is in the build folder.

Build application for software-in-the-loop (SIL) simulation
  • Set Model block parameter Code interface to Top model.

  • Set Hardware board to None.

  • For Linux, set Toolchain to one of these values:

    Automatically locate an installed toolchain
    GNU gcc/g++ | gmake (64-bit Linux)
    GNU gcc/g++ | CMake/gmake (64-bit Linux)
    GNU gcc/g++ | CMake/Ninja (64-bit Linux)

  • For Windows®, leave Toolchain set to the default value and select Enable portable word sizes.

On the development computer:

  • From a component model, create a test harness (requires Simulink® Test™) for the component and set Model block parameter Simulation mode to Software-in-the-loop (SIL).

  • From an architecture model (requires System Composer™), for each component Model block that you want to run in SIL mode, set block parameter Simulation mode to Software-in-the-loop (SIL).

  • Use the SIL/PIL Manager app to perform back-to-back SIL and normal mode simulations.

  • Development computer

  • Target computer (if it is the same computer as the development computer)

 
Build application in the context of a system
  • Set Hardware board to align with your target computer.

  • Set Toolchain to one of these values:

    Automatically locate an installed toolchain
    GNU gcc/g++ | gmake (64-bit Linux)
    GNU gcc/g++ | CMake/gmake (64-bit Linux)
    GNU gcc/g++ | CMake/Ninja (64-bit Linux)

On the development computer, with the architecture model selected:

  • On the Software Component Designer toolstrip, from the Generate Code menu, select Build.

  • Programmatically or at the MATLAB command line, use the slbuild function.

  • Use the Run on Custom Hardware app to build an executable program for running external mode simulations during which you can tune parameters in real time and monitor target application signals.

  • Use the Linux Runtime Manager (see Build application by using a Docker container)

Use the gmake or CMake command.

  • Development computer

  • Target computer

 
Build application by using a Docker container
  • Set Hardware board to Embedded Coder Linux Docker Container or Embedded Coder Linux Docker Container-ARM64.

  • Set Toolchain to Embedded Coder Linux Docker Container GCC toolchain.

On development computer, use the Linux Runtime Manager.

  • Development computer

  • Target computer

The Runtime Manager simplifies building, packaging, deploying, and running application software as a system.

Additional points to consider depending on your choices from the preceding table:

  • For builds in the MATLAB environment:

    • Set the Toolchain parameter to a value that includes CMake or gmake.

  • For builds outside of the MATLAB environment, for each software component model:

    • Run CMake in the generated folder application-name/exe or application-name/lib. CMake processes files in other folders automatically. If you build in the application-name/lib folder, the build produces a library instead of an executable program.

    • If you run CMake in the application-name/exe folder after running it in application-name/lib, the build replaces the existing library file with a new intermediate version.

  • For manual gmake builds:

    For each software component model, execute the command gmake -f makefilename for each of these folders in the folder order shown:

    1. Each application-name/subcomponent-name folder

    2. application-name/shared/lib folder

    3. application-name/application/lib folder

    4. application-name/exe folder

  • For builds when the Toolchain parameter is set to GNU gcc/g++ | CMake/gmake (64-bit Linux):

    • You can start a build by pressing CTRL+B or by using the slbuild function.

    • If Generate code only is selected, and you want to build manually, for each software component model, run CMake in each of these folders in the order shown:

      1. Each application-name/subcomponent-name folder

      2. application-name/shared/lib folder

      3. application-name/application-name/lib folder

      4. application-name/exe folder

      This results in an executable program build linking against the same library as a build for SIL simulation.

    • On Linux, for each application, the build process creates a shell script that lists CMake commands relevant for that folder.

Build Process Folder and File Structure and Naming

The build process places and names generated files in a default folder and file structure. Content of the structure varies depending on the build approach that you use. For example, if you generate source code only from a software component model, the structure only includes folders and files relevant for that application.

Default Folder and File Structure and Naming

For the most comprehensive build process, building an application in the context of a system, the code generator creates a folder that has the name of the architecture model. That folder stores application execution scheduling and service instance configuration manifest files. The manifest files are organized in a folder for each application (software component).

For example:

architecture-name
  application-name-1
  application-name-2
  application-name-3

These manifest files are in each application folder:

  • ExecutionManifest.json

  • ServiceInstanceManifest.json

For each software component model in the architecture model, the code generator creates a folder in your MATLAB® current folder for storing files that it generates for the application. This table lists paths for folders and files that the code generator creates.

FolderContent
application-name/application-nameBuild configuration files, such as buildinfo.mat and code descriptor codedescriptor.dmr, and the algorithm and service interface source (.cpp) and header (.h) files
application-name/exeExecutable program file and Make, data, and library files for building the executable
application-name/application-name/libAlgorithm library and data files
application-name/sharedShared utility source files
application-name/shared/libLibrary built from the source files in application-name/shared

The files application-name.h and application-name.cpp in folder application-name/application-name declare and define application entry-point functions, which are called from other application code. For example:

  • software-component-name_initialize

  • software-component-name_terminate

  • software-component-name_step

  • software-component-name_subsystem-name (exported)

These functions exchange data with target application code through service interfaces. The corresponding header files declare interfaces that the algorithm entry-point functions use to call other applications.

Customize Folder and File Structure and Naming

During a build, the code generator and build process place generated files into a default hierarchy of folders. Change the default structure and naming if:

  • You do not want to use the current working folder as the location for the code generation folder or the simulation cache folder.

  • You place the code generation folder under version control, but do not place the simulation cache folder under version control.

MATLAB session parameters are available for customizing the structure and naming. Parameters CacheFolder and CodeGenFolder are file generation control parameters that specify the folder locations for the build process. At the start of a MATLAB session, these Simulink settings determine the values of the parameters:

MATLAB Session ParameterSimulink PreferenceDescription
CacheFolderSimulation cache folder

Defines the root folder for simulation artifacts (for example, for Stateflow® and accelerator mode simulation ), and the Simulink cache file.

simulationCacheFolder

By default, when you set (CacheFolder = ''), simulationCacheFolder is the current working folder.

You can use the parameter to specify another folder. For example, if you want to:

  • Separate generated files from the model files and other source material.

  • Reuse or share previously built simulation targets without having to set the current working folder back to a previous working folder.

CodeGenFolderCode generation folder

Defines the root folder for generated code.

  • codeGenerationFolder/application-name/application-name

    Contains application source code.

  • codeGenerationFolder/application-name/application-name/lib

    Contains application library files.

  • codeGenerationFolder/application-name/exe/model-name.exe (Windows) or model-name (UNIX®)

    Contains application executable program.

You can use the parameter to specify another folder. For example, if you want to separate generated production code from:

  • Models and other source material

  • Generated simulation and code generation artifacts

If you specify the root folder of a drive (for example, C:\) as the code generation folder, the build process cannot generate code for your model.

Note

The MATLAB session parameter CodeGenFolderStructure and the Simulink preference Code generation folder structure are not supported for models configured to use service interfaces and application deployment.

If you want the parameter settings to persist across MATLAB sessions, adjust them in the Simulink Settings dialog box. To modify the parameter settings during a MATLAB session, use the function Simulink.fileGenControl. Setting changes that you make with the Simulink.fileGenControl function override the settings in the Simulink Settings dialog box. The parameter values that the function sets expire at the end of the MATLAB session.

For more information, see Build Process Support for File and Folder Names.

Application Build Limitation

You must compile generated application code on a Linux system. If you want to generate application code on a Windows development system, build the application by using the Linux Runtime Manager and a Docker container. Then, you can compile the code on a remote Linux development system or a Windows Subsystem for Linux system running an SSH server.

See Also

| | | | |

Topics