Main Content

C++ Code Generation

R2026b

You 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 the codegen command at the command line.

  • Set the TargetLang property 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.

GoalConfiguration ParameterMore Information

Produce C++ classes for classes in your MATLAB code.

Generate C++ classes from MATLAB classes

Generate C++ Code for MATLAB Classes

Generate entry-point functions as methods in a C++ class.

Interface style

Generate Class Interface in C++ Code

Generate C++ namespaces for MATLAB namespaces.

Generate C++ namespaces from MATLAB namespaces

Organize Generated C++ Code into Namespaces

Use different namespaces for the code that you write and MathWorks® code.Namespace for MathWorks code

Organize Generated C++ Code into Namespaces

Use C++ coder::array templates to represent dynamically allocated arrays. The generated code provides a simple API for interacting with this template.

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 enumerationCustomize 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 myType can appear in two different namespace hierarchies with top-level namespaces myNamespace_1 and myNamespace_2. The C language does not support namespaces and reuse of identifier names across hierarchies.

  • In generated C code, the function headers contain #ifdef __cplusplus include guards that declare the extern "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 .cpp file extensions for the C++ files and .h extensions for the header files. Generated C code uses .c and .h extensions.

  • 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 Inf and NaN based 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.

See Also

Topics