The build process requires a supported compiler. Compiler, in this context, refers to a development environment containing a linker and make utility, and a high-level language compiler. For details on supported compiler versions, see:
The build process also requires the selection of a toolchain or template makefile. The toolchain or template makefile determines which compiler runs, during the make phase of the build. For more information, see Configure Toolchain or Template Makefile Build Process.
To determine which templates makefiles are available for your compiler and system target file, see Compare System Target File Support Across Products.
For both generated files and user-supplied files, the file extension,
.cpp, determines whether the build
process uses a C or a C++ compiler. If the file extension is
build process uses C compiler to compile the file, and the symbols use the C linkage
convention. If the file extension is
.cpp, the build process uses a
C++ compiler to compile the file, and the symbols use the C++ linkage
The code generator produces code that is compliant with the following standards:
|Language||Supported Standard||Other Names|
Code that the code generator produces from these sources is ANSI® C/C++ compliant:
Simulink® built-in block algorithmic code
Generated system-level code (task ID [TID] checks, management, functions, and so on)
Code from other blocksets, including the Fixed-Point Designer™ product and the Communications Toolbox™ product
Code from other code generators, such as MATLAB® functions
Also, the code generator can incorporate code from:
Embedded system target files (for example, startup code, device driver blocks)
Custom S-functions or TLC files
Coding standards for these two sources are beyond the control of the code generator. These standards can be a source for compliance problems, such as code that uses C99 features not supported in the ANSI C, C89/C90 subset.
The code generator produces C and C++ code. Consider the following as you choose a programming language:
Does your project require you to configure the code generator to use a specific compiler? C/C++ code generation on Windows® requires this selection.
Does your project require you to change the default language configuration setting for the model? See Select C or C++ Programming Language.
Does your project require you to integrate legacy or custom code with generated code? For a summary of integration options, see What Is External Code Integration?.
Does your project require you to integrate C and C++ code? If so, see What Is External Code Integration?.
You can mix C and C++ code when integrating generated code with custom
code. However, you must be aware of the differences between C and
default C++ linkage conventions, and add the
"C"' linkage specifier where required. For the details of
the differing linkage conventions and how to apply
"C", refer to a C++ programming language reference
Does your project require code generation support from other products? See C++ Language Support Limitations.
For C++ code generation examples with Stateflow®, see the
sfcndemo_cppcount model or
To use C++ language support, you could need to configure the code generator to use
a specific compiler. For example, if a supported compiler is not installed on your
Windows computer, the default compiler is the
compiler shipped with the MATLAB product. This compiler does not support C++. If you do not configure
the code generator to use a C++ compiler before you specify C++ for code generation,
the software produces an error message.
Code generator limitations on C++ support include:
The code generator does not support C++ code generation for the following:
|Simscape Multibody™ First Generation (Simscape Multibody Second Generation is supported)|
|Simscape Electrical™ Power Systems|
For ERT and ERT-based system target files with Code interface
packaging set to
function, the following fields currently do not support
Data definition filename
Definition file for a data object, which is
an object of the
Simulink.Parameter classes or
If you specify a file name with a
.c extension or with
no extension and specify C++ for the code
generation language, the code generator produces a
The code generator reduces memory usage and enhances generated code execution by assuming signed integer C operations wrap on overflow. A signed integer overflow occurs when the result of an arithmetic operation is outside the range of values that the output data type can represent. The C programming language does not define the results of such operations. Some C compilers aggressively optimize signed operations for in-range values at the expense of overflow conditions. Other compilers preserve the full wrap-on-overflow behavior. For example, the gcc and MinGW compilers provide an option to wrap on overflow reliably for signed integer overflows. The generated program image for a model can produce results that differ from model simulation results because the handling of overflows varies, depending on your compiler.
When you generate code, if you use a supported compiler with the default options configured by the code generator, the compiler preserves the full wrap-on-overflow behavior. If you change the compiler options or compile the code in another development environment, it is possible that the compiler does not preserve the full wrap-on-overflow behavior. In this case, the executable program can produce unpredictable results.
If this issue is a concern for your application, consider one or more of the following actions:
Verify that the compiled code produces expected results.
If your compiler can force wrapping behavior, turn it on. For example, for
the gcc compiler or a compiler based on gcc, such as MinGW, configure the
build process to use the compiler option
Choose a compiler that wraps on integer overflow.
If you have Embedded Coder installed, develop and apply a custom code replacement library to replace code generated for signed integers. For more information, see Code Replacement Customization.
To view the installed compilers and select the default compiler, in the Command Window, type:
On a Windows computer, you can install supported compilers and select a default compiler.
On a UNIX® platform, the default compiler is GNU®
gcc/g++ for GNU or Xcode for Mac.
Unless the build approach configuration selects a specific compiler, the code generator uses the default compiler for the build process.
Primarily, the specified system target file determines the compiler that the code generator requires:
If you select a toolchain-based system target file such as
grt.tlc (Generic Real-Time Target),
ert.tlc (Embedded Coder®), or
autosar.tlc (Embedded Coder for AUTOSAR), the Build process
subpane displays configuration parameters for the build process. Use the
Toolchain configuration parameter to select a
compiler and associated tools for your model build. To validate the
selected toolchain, click the Validate
If you select a template makefile (TMF) based system target file, such
rsim.tlc, the Build
process subpane displays template makefile parameters for
configuring the build process. The Template
makefile parameter displays the default TMF file for the
selected system target file. If the system target file supports
compiler-specific template makefiles (for example, Rapid Simulation or
S-Function system target files), you can set Template
makefile to a compiler-specific TMF, such as
rsim_unix.tmf. (See Compare System Target File Support Across Products for valid TMF names.)
If the generated code exceeds the 2 Gb static memory limit, a compiler error occurs. To resolve this issue:
For GRT-based system target files, set model configuration parameter
Code interface packaging to
For ERT-based system target files, set model configuration parameter
Code interface packaging to
Reusable function and select parameter
Use dynamic memory allocation for model
When the code generator builds models with S-functions, source code for the
S-functions can be either in the current folder or in the same folder as their
MEX-file. The code generator adds an include path to the generated makefiles
whenever it finds a file named
in the same folder as
the S-function MEX-file. This folder must be on the MATLAB path.
Similarly, the code generator adds a rule for the folder when it finds a file
.cpp) in the same folder as the S-function MEX-file is