C++ Code Generation
R2026bYou can use MATLAB® Coder™ to generate C and C++ code. When you generate C++ code, the code generator uses C++ language features unavailable in C to produce code that is more readable and easier to integrate with existing C++ projects.
Generate C++ Code
The code generator produces C code by default. To generate C++ code, use one of these approaches:
Use the
-lang:c++option with thecodegencommand at the command line.Set the
TargetLangproperty in a code configuration object to"C++".In the Code Generation Settings, set the Language parameter to
C++.In the MATLAB Coder app, click the Language button in the MATLAB Coder toolstrip and select
C++.Set the target language to C++ in the MATLAB code by using
.coder.target
C++ Language Features Supported in Generated Code
This table describes some of the code configuration parameters that you can use to control the appearance and behavior of the generated C++ code.
| Goal | Configuration Parameter | More Information |
|---|---|---|
Produce C++ classes for classes in your MATLAB code. | Generate C++ classes from MATLAB classes | |
Generate entry-point functions as methods in a C++ class. | Interface style | |
Generate C++ namespaces for MATLAB namespaces. | Generate C++ namespaces from MATLAB namespaces | |
| Use different namespaces for the code that you write and MathWorks® code. | Namespace for MathWorks code | |
Use C++ | Dynamic memory allocation interface | Use Dynamically Allocated C++ Arrays in Generated Function Interfaces |
| If the target language standard is C++11 or later, generate C++ enumeration classes for MATLAB enumerations. | Generate C++ enum class from MATLAB enumeration | Customize Enumerated Types in Generated Code |
Differences Between Generated C Code and Generated C++ Code
If you separately generate C and C++ code for the same MATLAB function and inspect the generated source code, you see implementation differences. These are some notable differences:
The generated C++ code contains overloaded functions or methods that have the same name but support multiple signatures. The C language does not support overloading of functions.
The generated C++ code reuses the same identifier name across different namespace hierarchies. For example, the same type name
myTypecan appear in two different namespace hierarchies with top-level namespacesmyNamespace_1andmyNamespace_2. The C language does not support namespaces and reuse of identifier names across hierarchies.In generated C code, the function headers contain
#ifdef __cplusplusinclude guards that declare theextern "C"identifier for the generated C functions. The compiler and linker use these identifiers to build C code as part of a C++ project.Generated C++ code uses
.cppfile extensions for the C++ files and.hextensions for the header files. Generated C code uses.cand.hextensions.The generated C++ code uses some C++ casts, such as
static_cast, that are more explicit than the casting syntax in C.The generated code defines values for
InfandNaNbased on different mechanisms for C++ and C.Generated C++ code uses the custom data types as described in Mapping MATLAB Types to Types in Generated Code.
Generated C++ code uses different libraries that those used by the generated C code. See Change Language Standard Used for Code Generation.