主要内容

Generate Standalone C++ Code for Entry-Point Class at the Command Line

R2026b
Since R2026a

This example shows how to generate standalone C++ code for an entry-point class by using the codegen command. To learn how to generate code for an entry-point class by using the MATLAB® Coder™ app, see Generate Standalone C++ Code for Entry-Point Class Using MATLAB Coder App. For a detailed explanation of the steps of this workflow, including usage notes and limitations, see Code Generation for Entry-Point Classes.

Note

Using a MATLAB class as an entry point for code generation is a tech preview. This feature is in active development and might change between the tech preview and the general release. To enable the feature, enter enableCodegenForEntryPointClasses at the command line before launching the MATLAB Coder app, calling the codegen function, or creating a coder.Type object. To provide feedback, email the development team or participate in a survey.

Enable Tech Preview

Enable code generation for entry-point classes.

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

Examine MATLAB Class

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.

  • The isEqualArea method uses the getArea method to determine if two Rectangle objects have the same area.

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

To specify the Rectangle class as an entry point for code generation, create a coder.ClassSignature object that represents the Rectangle class. When you create a class signature object from the class name, the code generator does not populate the Properties structure because it infers the class properties from the class constructor method.

classSig = coder.ClassSignature("Rectangle")
classSig = 

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

Then, add the methods that you want to access from the custom C or C++ code to the class signature object by using the addMethod object function. You must add the class constructor 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.

Add the specifications for the Rectangle, getArea, and isEqualArea methods to the coder.ClassSignature object.

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

Inspect the coder.ClassSignature object. The object contains the specifications of 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 MEX Function and Test Generated Class

Generate a MEX function from the entry-point class. By default, calling the codegen command on an entry-point class generates a C MEX function with the same name as the MATLAB class in the working folder. Use the -class option to specify the class object, and use the -lang:c++ option to generate C++ code.

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

When you generate a MEX function for an entry-point class, the generated function contains the class and method definitions. To create an instance of the generated class, invoke the class constructor by passing the name of the constructor to the MEX function, followed by the required inputs.

Construct an instance of the Rectangle class.

myMexObj = Rectangle_mex("Rectangle",3,4);

The code generator creates an object that behaves like the MATLAB class but calls the MEX code.

To interact with this object, use MATLAB class syntax. For example, to verify that the getArea method produces the same output as the getArea method of the MATLAB class, use this command:

getArea(myMexObj)
ans =

    12

To test the isEqualArea method, create another instance of the MEX class.

myOtherMexObj = Rectangle_mex("Rectangle",2,6);

Call the isEqualArea method using both instances of the MEX class. The generated method produces the expected output.

isEqualArea(myMexObj,myOtherMexObj)
ans =

  logical

   1

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 external applications can use to interface with the C++ class. The init method and non-initializing constructor are internal methods. Do not use internal methods 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