Create C++ MATLAB Data API Shared Library Header from Strongly Typed MATLAB Classes Contained in Package
This example shows how to create a C++ shared library header from strongly typed MATLAB® classes contained within a package and integrate it with sample C++ application code.
Start this example by creating a new work folder that is visible to the MATLAB search path.
Verify that you have a C++ compiler installed by typing
mbuild -setupat the MATLAB command prompt.
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.
Location of Example Files
Purpose of Each Example File
|Package containing two classes:
|A class within the |
|A class within the |
|A function that accepts a |
|C++ application code that integrates the header file generated by compiling 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 Classes and Functions in MATLAB
Examine the code for
+shapespackage contains two MATLAB classes:
calculatearea.mMATLAB function located outside of the
+shapespackage accepts a
MyRectangleobject as input and calculates the area of the rectangle.
Create a MATLAB script named
runshapes.mwith 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
Generate C++ Shared Library Header Using the
if ~exist('output/cpp','dir') mkdir output/cpp end mcc -W 'cpplib:libshapes,generic' +shapes/MyPosition.m +shapes/MyRectangle.m calculatearea.m -d output/cpp
The following files are created in the
For details, see Data Type Mappings Between C++ and Strongly Typed MATLAB Code.
Integrate C++ MATLAB Data API Shared Library Header with C++ Application
Examine the C++ application code contained in the
When writing C++ application code, you must include the header file (
.hppfile) generated by the
mcccommand or the Library Compiler app and the
MatlabCppSharedLib.hppheader file using
Compile and link the C++ application at the MATLAB command prompt.
mbuild shapes_mda.cpp -outdir output/cpp
Run the application from the system command prompt by passing the deployable archive (
.ctffile) as an input. Before running the application at the system command prompt, verify that you have MATLAB Runtime installed on your machine prior to running the application at the system command prompt. For details, see Install and Configure MATLAB Runtime.
For testing purposes, you can run the application from the MATLAB command prompt.
Rectangle 1 Point (10.000000, 5.000000) Point (50.000000, 20.000000) Rectangle (10.000000, 5.000000) -> (50.000000, 20.000000) Rectangle 2 Point (0.000000, -5.000000) Point (60.000000, 30.000000) Rectangle (0.000000, -5.000000) -> (60.000000, 30.000000) Area of rectangle r1 = 600 Area of rectangle r2 = 2100