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.

Generate C++ Code

To generate C++ code, follow the same overall workflow steps that you use to generate C code. For example, see Generate C Code at the Command Line. Select the C++ language option from the command line, or with a code generation configuration setting, or from the MATLAB Coder app.

Suppose that you want to generate C++ code for a function foo that 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 for foo, enter:

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

    cfg = coder.config('dll');
    cfg.TargetLang = 'C++'; 
    codegen -config cfg foo 
    
  • From the app, at the Generate Code step, select the C++ language button.

C++ Language Features Supported in Generated Code

You can generate C++ code that uses a limited subset of C++ language features.

Namespaces

By generating code with a namespace, you can more easily integrate your code with other source code that might have identical function or data type names. When you use a namespace, the code generator packages all the generated functions and type definitions into the namespace, except for the generic type definitions contained in tmwtypes.h and the hardware-specific definitions in rtwtypes.h. The example main file and function are not packaged into the namespace.

Specify the namespace by using the CppNamespace configuration object option. For example, to generate C++ code in a namespace called generated, enter:

cfg = coder.config('dll');
cfg.TargetLang = 'C++';
cfg.CppNamespace = 'generated'; 
codegen -config cfg foo 

To specify a namespace from the app, at the Generate Code step, select More Settings > All Settings, and then modify the C++ Namespace field. For an example that uses namespaces, see Integrate Multiple Generated C++ Code Projects.

Class Interface

To attain more object-oriented code, you can generate C++ code with a class interface. The entry-point function or functions are produced as methods in a C++ class. Specify the class interface by using the CppInterfaceStyle property. Designate the name of the generated class with CppInterfaceClassName. For example:

cfg = coder.config('lib');
cfg.GenCodeOnly = true;
cfg.TargetLang = 'C++';
cfg.CppInterfaceStyle = 'Methods';
cfg.CppInterfaceClassName = 'myClass';
codegen foog -config cfg -report -d withClass

For more information, see Generate C++ Code with Class Interface.

The code generator does not support the generation of a C++ class directly from a MATLAB class.

Differences in 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:

  • In generated C code, the function headers contain #ifdef __cplusplus include guards that specify the extern "C" identifier for the generated C functions. The compiler and linker use these identifiers in building 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, like static_cast, which 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 than generated C code. For example, the default standard math library for C++ and C is described in Change the Standard Math Library.

See Also

Related Topics