Main Content

Generate Standalone Code for Entry-Point Class (Tech Preview)

Since R2026a

Note

Using a MATLAB® class as an entry point for code generation is a tech preview. This feature is in active development and may change between the tech preview and the general release. The primary purpose of the tech preview is to solicit feedback from users. To enable the feature, enter enableCodegenForEntryPointClasses at the command line before calling the codegen function or creating a coder.Type object in a MATLAB session. To provide feedback, email the development team or participate in a survey.

You can use a MATLAB value class or handle class as an entry point for standalone code generation. An entry-point class is a class that you want to access directly from your custom C or C++ code. The code generator preserves the signatures of classes identified as entry points. Entry-point classes are stable interfaces to the generated code. For classes that you do not designate as entry points, the code generator uses internal heuristics to balance code performance and readability. This means that the code generator can eliminate, inline, or modify non-entry-point classes. Classes that you do not designate as entry points are not stable interfaces to the generated code.

When you generate C or C++ code for an entry-point class:

  • The MATLAB Coder™ app is not supported. You must generate code by using the codegen command.

  • MEX function generation is not supported. You must generate standalone code.

  • Software-in-the-loop (SIL) and processor-in-the-loop (PIL) verification are not supported. Interact with the generated C++ class manually to test its behavior.

Use Class as Entry Point

To generate code for a MATLAB class as an entry point, follow these steps:

  1. Enable the tech preview feature by entering enableCodegenForEntryPointClasses at the command line. You must enable the feature before calling the codegen function or creating a coder.Type object in a MATLAB session.

  2. Create a coder.ClassSignature object that represents a MATLAB class.

  3. Add some or all of the public methods of the MATLAB class to the coder.ClassSignature object. At a minimum, you must add the class constructor method.

  4. Generate code by using the codegen command. Specify the entry-point class by using the -class option.

Usage Notes and Limitations

When you generate C or C++ code for an entry-point class, you must be aware of certain usage notes and limitations:

  • An entry-point class must be a coder.ClassSignature object with methods specified as coder.FunctionSignature objects.

  • To generate code for a MATLAB class, pass the coder.ClassSignature object representing the class to the codegen command by using the -class option. Specify multiple entry-point classes as an array of coder.ClassSignature objects or by using the -class option multiple times.

  • The code generator only generates stable interfaces for methods that you add to the coder.ClassSignature object. Other methods of the MATLAB class can be changed or inlined by the code generator. To call a class method directly from your custom C or C++ code, you must add it to the coder.ClassSignature object that represents the class.

  • You must specify the types of all class properties in the class constructor method, including all structure fields, cell array elements, and properties of contained classes. If the code generator is unable to determine the types of one or more properties of the entry-point class before you use the property, code generation fails. See Resolve Error: Variables Must Be Fully Defined Before Use.

  • You can only add public methods to a coder.ClassSignature object. The code generator infers private and protected method definitions from the MATLAB code.

  • If an entry-point class is a value class, the generated code passes the class by reference. By contrast, the generated code passes value classes that are not entry points by value. Therefore, if you specify a value class as an entry point, and you also use a method of the value class in an entry-point function, the code generator produces two versions of the method in the generated code. The generated code passes the class by value in one generated method and passes the class by reference in the other generated method.

  • You must add a class constructor method to the coder.ClassSignature object. If the MATLAB class does not define a constructor, add the default constructor. See Class Constructor Methods.

  • You can use many System objects as entry points for code generation. To generate code for an entry-point System object™, add the public step method to the coder.ClassSignature object. Because step supports a variable number of outputs, specify the number of output arguments when you add the method. See Generate C++ Executable for Entry-Point System Object in a Namespace (Tech Preview).

  • If a property of an entry-point class is a handle class, that property must be private or protected. If the class with the handle class property is itself nested within one or more classes, the top-level enclosing class must also be specified as an entry point.

  • To define a property of an entry-point class as a constant, use the Constant attribute in the MATLAB class definition. Code generation does not support coder.Constant for properties of entry-point class objects.

  • When a method of a coder.ClassSignature object takes another coder.ClassSignature object as an input argument, you must specify both coder.ClassSignature objects as entry points by using the -class option with the codegen command.

  • When generating code for an entry-point class, the code generator does not produce an example main function.

  • Code generation supports entry-point classes with polymorphic methods. When you generate C code, you must distinguish polymorphic methods by using the InterfaceName property of the coder.FunctionSignature object.

  • You cannot generate multi-instance, reentrant code for an entry-point class.

  • Code generation does not support circular dependencies among entry-point classes.

  • If you specify multiple identical coder.ClassSignature objects as entry points, the coder.ClassSignature objects must have handle equality. See Equality of Handle Objects.

  • These types of classes are not supported as entry points:

    • Abstract classes

    • Classes contained within aggregate data types, such as structures, cells, and other classes

    • Classes that inherit from the matlab.mixin.Copyable class

  • You cannot add these types of methods to a coder.ClassSignature object:

Example: Generate Standalone C++ Code for Entry-Point Class

This example shows how to generate C++ code for an entry-point class.

Enable Feature and Examine MATLAB Class

First, enable code generation for entry-point classes.

enableCodegenForEntryPointClasses
=== Code generation for entry-point classes is ENABLED ===

To use the Classes As Entry-Points feature, restart MATLAB and rerun 'enableCodegenForEntryPointClasses' before calling codegen or coder.Type.

For a feature overview and example usage, see the R2026a pre-release Release Notes.

To send feedback or questions directly to the development team, email entrypointclassfeedback@groups.mathworks.com or click here to take survey.

Examine the Rectangle class, which defines a Rectangle object with a length and width specified by the properties Length and Width. The Rectangle class has three methods. The Rectangle constructor creates the object, the getArea method calculates the area of a Rectangle object, and the isEqualArea method uses the getArea method to determine if two Rectangle objects have the same area.

type Rectangle.m
classdef Rectangle
    properties
        Length
        Width
    end
    methods
        function obj = Rectangle(length,width)
            obj.Length = length;
            obj.Width = width;
        end
        function area = getArea(obj)
            area = obj.Length*obj.Width;
        end
        function out = isEqualArea(obj1,obj2)
            out = isequal(getArea(obj1),getArea(obj2));
        end
    end
end

Define coder.ClassSignature Object

Define a coder.ClassSignature object that represents the Rectangle class.

classSig = coder.ClassSignature("Rectangle")
classSig = 
coder.ClassSignature
  1×1 Rectangle
      TypeName: "Rectangle"
    Properties: struct with no fields.
   Methods: dictionary (string --> cell) with no entries.

Add methods to the object by using the addMethod method. The code generator represents the methods of a coder.ClassSignature object as coder.FunctionSignature objects. When you add a method that takes an instance of the enclosing class as an argument, use the coder.ClassSignature object that represents the enclosing class.

For this example, add the specifications for the Rectangle, getArea, and isEqualArea methods to the coder.ClassSignature object directly.

addMethod(classSig,"Rectangle",{0,0});
addMethod(classSig,"getArea",{classSig});
addMethod(classSig,"isEqualArea",{classSig,classSig});

Inspect the coder.ClassSignature object. The object contains the Rectangle, getArea, and isEqualArea methods. The coder.ClassSignature object represents method arguments that are instances of the enclosing class by using the keyword this.

classSig
classSig = 
coder.ClassSignature
  1×1 Rectangle
      TypeName: "Rectangle"
    Properties: struct with no fields.
       Methods:
         Rectangle:
           Args: {1×1 double, 1×1 double}
         getArea:
           Args: {1×1 this}
         isEqualArea:
           Args: {1×1 this, 1×1 this}

Generate and Inspect Standalone C++ Code

Generate a C++ static library for the Rectangle class by passing the coder.ClassSignature object to the codegen command. Use the -class option to specify the class object, and use the -lang:c++ option to generate C++ code.

codegen -config:lib -lang:c++ -class classSig
Code generation successful.

Inspect the definition of the Rectangle class in the generated C++ header file Rectangle.h. The Rectangle, getArea, and isEqualArea methods are public C++ methods that the external application can use to interface with the C++ class. The init method and non-initializing constructor are internal methods. Because these methods may change in the future, do not use them in your custom C++ code.

file = fullfile("codegen","lib","Rectangle","Rectangle.h");
coder.example.extractLines(file,"// Type Definitions","};",0,1)
class Rectangle {
public:
  void init(double length, double width);
  double getArea() const;
  boolean_T isEqualArea(const Rectangle *obj2) const;
  Rectangle(double length, double width);
  Rectangle();
  ~Rectangle();
  double Length;
  double Width;
};

See Also

| | |

Topics