Main Content

Publish Interface to Header-Only C++ Library

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 package to create Student and Teacher objects and specify names and ages.

Verify Selected C++ Compiler

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

Copy Example Header File

Copy the school.hpp header file to a writable folder on your path. This example assumes that the current folder is H:\Documents\MATLAB\publisher.


Generate Definition File

Use the clibgen.generateLibraryDefinition function to create a library definition file defineschool.mlx.

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).

Define Missing Information for C++ Function getName

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 defineschool.mlx in 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". Uncomment the statements in the getName code section.

The input argument p is a scalar value. Replace <SHAPE> in this statement with the number 1:

defineArgument(getNameDefinition, "p", "", "input", <SHAPE>);
defineArgument(getNameDefinition, "p", "", "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 Interface and Add to MATLAB Path

Call the build function to create the interface file schoolInterface.dll in the H:\Documents\MATLAB\publisher\school folder.

Building interface file 'schoolInterface.dll'.
Interface file 'schoolInterface.dll' built in folder 
To use the library, add the interface file folder to the MATLAB path.

Click the interface file folder link to add the interface to the path. Alternatively, type:


View Contents of Library

The summary method displays a summary of the C++ classes and functions defined in the school interface.

MATLAB Interface to school Library



    string getName()
    uint64 getAge()

  No Properties defined



    string getName()

  No Properties defined



    string getName()

  No Properties defined


Test Functions

To call functionality in the school interface, use the MATLAB clib package. Type the package name, ending 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 ='Ms. Jones',24);

Use getName to display the teacher's name.

ans = "Ms. Jones"

Modify Help Text

You can provide help text for your users. For more information, see Publish Help Text for MATLAB Interface to C++ Library.

Distribute Interface

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

See Also

| | |

Related Topics