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.
Prerequisites
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 Set 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 Download and Install MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime when running the C++ application.
Files
Location of Example Files
Example Files |
|
Purpose of Each Example File
Files | Purpose |
---|---|
+shapes | Package containing two classes:
MyPosition.m and
MyRectangle.m . |
MyPosition.m | Class within the +shapes package that
accepts the X and Y coordinates of a point and creates a
MyPosition object. |
MyRectangle.m | Class within the +shapes package that
accepts two points specified as MyPosition
objects and creates a MyRectangle
object. |
calculatearea.m | Function that accepts a MyRectangle object
as input and calculates the area of the rectangle. |
shapes_mda.cpp | C++ 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'); copyfile(appDir)
Create MATLAB Function and Classes
Examine the code for
MyPosition.m
,MyRectangle.m
, andcalculatearea.m
.The
+shapes
package contains two MATLAB classes:MyPosition.m
andMyRectangle.m
.The
calculatearea.m
MATLAB function located outside of the+shapes
package accepts aMyRectangle
object as input and calculates the area of the rectangle.
Established MATLAB users may find it unconventional to see a
properties
block in a class and anarguments
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 adouble
data type representing a value, you can now represent that in MATLAB as adouble
. You can also specify a MATLAB object as an argument or property type. For example, theMyRectangle
class specifiesshapes.MyPosition
as the type for theUpperLeft
andLowerRight
properties of theMyRectangle
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
andarguments
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.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.runshapes
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 compiler.build.cppSharedLibrary
Create a code archive (.ctf
file) and header
(.hpp
file) from the MATLAB function and classes using the compiler.build.cppSharedLibrary
function.
files = ["calculatearea.m", "+shapes"]; buildResults = compiler.build.cppSharedLibrary(files, OutputDir="output", LibraryName="libshapes", Verbose="on")
The function generates the following files in a folder named
output
in your current working directory.
P:\MATLAB\WORK\OUTPUT │ GettingStarted.html │ includedSupportPackages.txt │ mccExcludedFiles.log │ readme.txt │ requiredMCRProducts.txt │ unresolvedSymbols.txt │ └───v2 └───generic_interface libshapes.ctf libshapesv2.hpp readme.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 Map 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 Set 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.
In the work folder for this example create a new file named
shapes_mda.cpp
with the following code: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.
setenv("CPPSHARED_BASE_CTF_PATH","P:\MATLAB\work\strongly_typed\output\v2\generic_interface")
If you're using Visual Studio, see Set Environment Variables 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.
!output\bin\shapes_mda.exe
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