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.
Prerequisites
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 -setup
at 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.
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 | A class within the +shapes package that
accepts the X and Y coordinates of a point and creates a
MyPosition object |
MyRectangle.m | A class within the +shapes package that
accepts two points specified as MyPosition
objects and creates a MyRectangle
object |
calculatearea.m | A function that accepts a MyRectangle
object as input and calculates the area of the rectangle |
shapes_mda.cpp | 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
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.
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
Generate C++ Shared Library Header Using the mcc
Command
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
output
>cpp
>v2
>generic_interface
folder:
readme.txt
libshapes.ctf
libshapesv2.hpp
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
shapes_mda.cpp
file.Note
When writing C++ application code, you must include the header file (
.hpp
file) generated by themcc
command or the Library Compiler app and theMatlabCppSharedLib.hpp
header file using#include
directives.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 (
.ctf
file) 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.
!output\cpp\shapes_mda.exe output\cpp\v2\generic_interface\libshapes.ctf
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