Main Content

Deploy MATLAB Classes to C++ Application Using MATLAB Data API

This example shows how to package MATLAB® classes contained within a MATLAB package and deploy it to a C++ application. It uses the MATLAB Data API for managing data exchange between the MATLAB code and the C++ application. The MATLAB classes are accessed via a front-facing MATLAB function. The workflow is supported on Windows®, Linux®, and macOS.


  • Create a new work folder that is visible to the MATLAB search path. This example uses a folder named work.

  • Verify that you have set up a C++ development environment. For details, see Setting Up C++ Development Environment. This example uses MATLAB as a C++ development environment. Therefore, verify that you have a C++ compiler installed by typing mbuild -setup C++ at the MATLAB command prompt.

  • Verify that you have met all of the MATLAB Compiler SDK™ C++ target requirements. For details, see MATLAB Compiler SDK C++ Target Requirements.

  • End users must have an installation of MATLAB Runtime to run the application. For details, see Install and Configure MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime when running the C++ application.


Location of Example Files

Example Files


Purpose of Each Example File

+shapesPackage containing two classes: MyPosition.m and MyRectangle.m.
MyPosition.mClass within the +shapes package that accepts the X and Y coordinates of a point and creates a MyPosition object.
MyRectangle.mClass within the +shapes package that accepts two points specified as MyPosition objects and creates a MyRectangle object.
calculatearea.mFunction that accepts a MyRectangle object as input and calculates the area of the rectangle.
shapes_mda.cppC++ application code that integrates the code archive (.ctf file) header (.hpp file) generated by packaging the MATLAB code.

Copy the example files to the current work folder.

appDir = fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','strongly_typed');

Create MATLAB Function and Classes

  1. Examine the code for MyPosition.m, MyRectangle.m, and calculatearea.m.

    • The +shapes package contains two MATLAB classes: MyPosition.m and MyRectangle.m.

    • The calculatearea.m MATLAB function located outside of the +shapes package accepts a MyRectangle object as input and calculates the area of the rectangle.

     +shapes (Package)

     MyPosition.m (Class)

     MyRectangle.m (Class)

     calculatearea.m (Function)

    Established MATLAB users may find it unconventional to see a properties block in a class and an arguments block in a method or function, each detailed with data type information. Both blocks let you represent C++ data types with an equivalent MATLAB type. For instance, if your C++ application employs a double data type representing a value, you can now represent that in MATLAB as a double. You can also specify a MATLAB object as an argument or property type. For example, the MyRectangle class specifies shapes.MyPosition as the type for the UpperLeft and LowerRight properties of the MyRectangle class. This option to specify types is useful in situations where a C++ application has strict type requirements. For details, see Data Type Mappings Between C++ and Strongly Typed MATLAB Code.

    In this example, properties and arguments blocks with data type information are used to illuminate subtle nuances. However, remember that including type information is entirely optional. The deployment process remains unchanged even without it. Various parts of this example underscore the areas where this difference manifests. So, if data types aren't crucial in your deployment, the specification of type information is not necessary.

  2. Create a MATLAB script named runshapes.m with the following code and execute it at the MATLAB command prompt. This script illustrates how the classes and function interact to generate an output.


    Rectangle 1
    Point 1 = (10.000000,5.000000)
    Point 2 = (50.000000,20.000000)
    Rectangle (10.000000,5.000000) -> (50.000000,20.000000)
    Rectangle 2
    Point 1 = (0.000000,-5.000000)
    Point 2 = (60.000000,30.000000)
    Rectangle (0.000000,-5.000000) -> (60.000000,30.000000)
    Area of rectangle r1 = 600
    Area of rectangle r2 = 2100

Create C++ Shared Library Using

Create a code archive (.ctf file) and header (.hpp file) from the MATLAB function and classes using the function.

files = ["calculatearea.m", "+shapes"];
buildResults =, OutputDir="output", LibraryName="libshapes", Verbose="on")

The function generates the following files in a folder named output in your current working directory.

│   GettingStarted.html
│   includedSupportPackages.txt
│   mccExcludedFiles.log
│   readme.txt
│   requiredMCRProducts.txt
│   unresolvedSymbols.txt

To finalize integration, you need the libshapes.ctf code archive file and the libshapesv2.hpp header file from the generic_interface folder. You can view the header file here:


For an in-depth discussion of how theMATLAB classes and function and mapped to C++ in the header file, see Mapping MATLAB Classes and Functions to C++.

For details on data type mappings, see Data Type Mappings Between C++ and Strongly Typed MATLAB Code.

Integrate MATLAB Code Archive into C++ Application

You can finalize the integration process in your preferred C++ development environment, including MATLAB or alternatives such as Microsoft® Visual Studio® on Windows. This example, however, uses MATLAB as a C++ development environment. For details, see Setting Up C++ Development Environment.

To integrate the generated MATLAB code archive (.ctf file) and header (.hpp file) into a C++ application, adhere to these guidelines:

  • Use a #include directive to incorporate the generated header file (.hpp file) in your C++ application code.

  • Ensure the code archive (.ctf file) is positioned in a location that the C++ executable can access.

Completing the integration step requires proficient C++ skills for writing application code. You can use the following sample C++ application code as guide when writing your own application.

  1. In the work folder for this example create a new file named shapes_mda.cpp with the following code:


  2. Compile and link the application by executing the mbuild function at the MATLAB command prompt.

    mbuild -v shapes_mda.cpp -outdir output\bin

Handling the Code Archive (.ctf file)

To ensure your C++ application can access the code archive (.ctf file) containing MATLAB code, place the file in a location accessible to the executable. For this example we are going to do this by setting the CPPSHARED_BASE_CTF_PATH environment variable in the MATLAB desktop environment.


If you're using Visual Studio, see Setting Environment Variable in Visual Studio.

For a complete list of code archive (.ctf file) placement options, see Code Archive (.ctf file) Placement.

Run C++ Application

For testing purposes, you can run the application from the MATLAB command prompt. This does not require a MATLAB Runtime installation.

Rectangle 1
Point (10.000000, 5.000000)
Point (50.000000, 20.000000)
Rectangle (10.000000, 5.000000) -> (50.000000, 20.000000)
Area of rectangle r1 = 600
Rectangle 2
Point (0.000000, -5.000000)
Point (60.000000, 30.000000)
Rectangle (0.000000, -5.000000) -> (60.000000, 30.000000)
Area of rectangle r2 = 2100

See Also


Related Topics