Publish MATLAB Interface to C++ Library

To create a MATLAB® interface to a C++ library, use the clibgen package

To build (publish) a MATLAB interface to a C++ library, you need:

  • One or more header files that contain declarations of all the functions exported by the library. You should be able to compile the headers in a C++ development environment and use the functionality in C++ applications.

  • A shared library file — .dll on Windows®, .so on Linux®, or .dylib on macOS. If the library is completely defined in the header files, then the library file is optional.

  • Additionally, an import library file on Windows — .lib

  • A MATLAB-supported C++ compiler.

The MATLAB interface automatically converts C++ function signatures into MATLAB function signatures. However, some C++ language constructs do not have unique matches in the MATLAB language. To bridge this gap, MATLAB uses a definition file (with the .mlx file extension) which a publisher creates and modifies to provide missing information. The publisher must have enough C++ language skills to interpret a function signature and provide the missing information.

An example of information the publisher needs to define relates to the use of pointers to pass data to functions. A pointer is a location in memory that indicates the start of a block of data. To pass this data to MATLAB safely, the publisher must specify the size of the data. Generally, the function documentation indicates the size of the data, perhaps as an additional input argument. Using the MATLAB definition file, the publisher specifies the value, and then MATLAB creates the equivalent MATLAB function signature. To display function signatures, see Display Help for MATLAB Interface to C++ Library.

To make this interface available to other MATLAB users, provide them with the MATLAB interface file. The end user puts the shared library file on the system path or run-time path and the interface file on the MATLAB path.

For information about calling functions in the library, see Call C++ Functions from Published MATLAB Interface.


clibgen.generateLibraryDefinitionCreate definition file for C++ library
clibgen.buildInterfaceCreate interface to C++ library without definition file


clibgen PackageSummary of packages and classes to support calling C++ library functionality from MATLAB


System Requirements

Requirements for calling functions in external, shared C++ libraries from MATLAB.

Steps to Publish a MATLAB C++ Library Interface

Outline for publishing an interface to a C++ library.

Define MATLAB Interface to C++ Library

How to update definitions in the MATLAB .mlx definition file.

Sample C++ Library Definition File

Create library definition file from example cppUseCases.hpp header file.

Publish Interface to Header-Only C++ Library

This example creates a MATLAB interface to a C++ library defined in a header file school.hpp.

Publish Interface to Shared C++ Library on Windows

This example creates a MATLAB interface to a Windows C++ library matrixOperations.

Publish Interface to Shared C++ Library on Linux

This example creates a Linux version of the MATLAB interface to C++ library matrixOperations.

Publish Modified Help Text

When you publish an interface, the clibgen.generateLibraryDefinition function inserts default text about classes and functions.


Limitations to C/C++ Support

C/C++ library features not supported in MATLAB.

Troubleshooting C++ Interface Issues

Information for resolving C++ interface issues.

Errors Parsing Header Files on macOS

Work around for errors parsing header file on macOS.

Build Error Due to Compile-Time Checks

How to remove C++ members containing compile-time assertions.