Main Content

C++ Code Generation

MATLAB®Coder™enables you to either generate C or C++ code. The code generator produces C code by default. Generated C++ code can use functionality not available in the C language that can make the C++ code more readable and easier to use.

基因rate C++ Code

To generate C++ code, follow the same overall workflow steps that you use to generate C code. For example, see基因rate C Code at the Command Line. Select the C++ language option from the command line, or with a code generation configuration setting, or from theMATLAB Coder应用程序。

Suppose that you want to generate C++ code for a functionfoothat accepts zero inputs:

  • From the command line, use the-lang:c++specifier. This specifier provides a quick and easy way to generate C++ code. For example, to generate a C++ static library and C++ source code forfoo, enter:

    codegen-config:lib-lang:c++foo
  • In the configuration object, set theTargetLangparameter to C++. For example, to generate a C++ dynamic library, enter:

    cfg = coder.config('dll'); cfg.TargetLang ='C++'; codegen-configcfgfoo
  • From the app, at the基因rate Codestep, select theC++language button.

C++ Language Features Supported in Generated Code

To learn about code generation that utilizes key C++ language features, refer to these help topics:

Goal More Information

基因rate C++ classes for classes in your MATLAB code.

基因rate C++ Classes for MATLAB Classes

基因rate entry-point functions as methods in a C++ class.

基因rate C++ Code with a Class Interface

基因rate C++ namespaces for MATLAB packages. Place all generated code in a namespace that you specify. Place all code generated for MathWorks®code in a namespace that you specify.

Organize Generated C++ Code into Namespaces

Pass dynamically allocated arrays between your custom C++ code and the generated code. The generated C++ code implements such arrays by using thecoder::arrayclass template. The generated code provides a simple API that you can use to interact with this template.

Use Dynamically Allocated C++ Arrays in Generated Function Interfaces

These examples illustrate the use of these functionalities:

Additional Differences Between Generated C Code and C++ Code

If you separately generate C and C++ code for the same MATLAB function, and inspect the generated source code, then there are 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 namemyTypecan appear in two different namespaces hierarchies with top-level namespacesmyNamespace_1andmyNamespace_2. The C language does not support namespaces and such reuse of identifier names.

  • In generated C code, the function headers contain#ifdef __cplusplusinclude guards that specify theextern "C"identifier for the generated C functions. The compiler and linker use these identifiers in building C code as part of a C++ project.

  • 生成c++代码使用.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, likestatic_cast, which are more explicit than the casting syntax in C.

  • The generated code defines values forInfandNaNbased on different mechanisms for C++ and C.

  • 生成c++代码使用the custom data types as described inMapping MATLAB Types to Types in Generated Code.

  • 生成c++代码使用different libraries than generated C code. For example, the default language standard for C++ and C is described inChange the Language Standard.

See Also

Related Topics