Build 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 or source files that contain declarations of all the functions exported by the library. You should be able to compile these files 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 or source 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 Use Prebuilt MATLAB Interface to C++ Library.

Functions

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

Packages

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

Topics

Requirements for Building Interface to C++ Libraries

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

Steps to Publish a MATLAB Interface to a C++ Library

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

Define MATLAB Interface for C++ Library

Generate and edit the MATLAB .mlx definition file to customize and define the interface.

Publish Help Text for MATLAB Interface to C++ Library

Provide documentation for end-users of a MATLAB interface to a C++ Library.

Build C++ Library Interface and Review Contents

If library functionality is missing, the library might contain unsupported language features or data types.

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 C++ Library Using Source Files

This example creates a MATLAB interface to a C++ library defined in a source files matrixOperations.cpp.

Publish Interface to Shared C++ Library on Windows

This example creates a Windows version of the MATLAB interface to 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.

Troubleshooting

Limitations to C/C++ Support

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

C++ Limitation Workaround Examples

Example code to work around C++ library limitations.

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.