This example creates a MATLAB® interface to a C++ library named
school. The library is
defined in the header file
school.hpp and does not have a shared library
file. A library defined completely by its header file is called a
header-only library. If your library includes a shared library file,
then see Publish Interface to Shared C++ Library on Windows or Publish Interface to Shared C++ Library on Linux.
This library defines classes representing students and teachers. After you publish this
library, MATLAB users can call functions in the
clib.school package to create
Teacher objects and specify names and
Verify that you have selected a C++ compiler. For an up-to-date list of supported compilers, see Supported and Compatible Compilers. This example uses the MinGW64 compiler.
mex -setup cpp
school.hpp header file to a writable folder on your path.
This example assumes that the current folder is
clibgen.generateLibraryDefinition function to create a library definition file
Using MinGW64 Compiler (C++) compiler. Generated definition file defineschool.mlx and data file 'schoolData.xml' contain definitions for 21 constructs supported by MATLAB. 1 construct(s) require(s) additional definition. To include these construct(s) in the interface, edit the definitions in defineschool.mlx. Build using build(defineschool).
MATLAB reports that one construct requires additional information (definition). This
means that MATLAB cannot automatically define the signature for one of the functions. To provide
the missing information, click the link to open
MATLAB Live Editor.
Constructs with missing information are commented out. Scroll through the file to locate
the section titled "C++ function getName with MATLAB name clib.school.getName". Uncomment
the statements in the
getName code section.
The input argument
p is a scalar value. Replace
<SHAPE> in this statement with the number
defineArgument(getNameDefinition, "p", "clib.school.Person", "input", <SHAPE>);
defineArgument(getNameDefinition, "p", "clib.school.Person", "input", 1);
Save the definition file.
Validate the library.
For more information about including constructs in an interface, see Define Missing Information for MATLAB Signatures.
build function to
create the interface file
schoolInterface.dll in the
Building interface file 'schoolInterface.dll'. Interface file 'schoolInterface.dll' built in folder 'H:\Documents\MATLAB\publisher\school'. To use the library, add the interface file folder to the MATLAB path.
interface file folder link to add the interface to the
path. Alternatively, type:
displays a summary of the C++ classes and functions defined in the
MATLAB Interface to school Library Class clib.school.Person Constructors: clib.school.Person() clib.school.Person(string,uint64) clib.school.Person(clib.school.Person) Methods: setName(string) setAge(uint64) string getName() uint64 getAge() No Properties defined Class clib.school.Teacher Constructors: clib.school.Teacher() clib.school.Teacher(string,uint64) clib.school.Teacher(clib.school.Teacher) Methods: string getName() No Properties defined Class clib.school.Student Constructors: clib.school.Student() clib.school.Student(string,uint64) clib.school.Student(clib.school.Student) Methods: string getName() No Properties defined Functions string clib.school.getName(clib.school.Person)
To call functionality in the
school interface, use the MATLAB
clib package. Type the package name
with a period. You can press Tab to see the three classes and one function
defined in the library.
Once you use a library class or function, you cannot modify the library definition unless you restart MATLAB and rebuild the library.
Create a teacher.
t1 = clib.school.Teacher('Ms. Jones',24);
getName to display the teacher's name.
ans = "Ms. Jones"
You can provide help text for your users. For more information, see Publish Help Text for MATLAB Interface to C++ Library.
To give the interface to another MATLAB user, instruct them to add the
schoolInterface.dll file to
a folder named
school and add the folder to the MATLAB path. The package name is