Main Content

Generate Interface

This example shows how to create a MATLAB® library definition file for the C++ library declared in the header file matrixOperations.hpp and defined in the C++ source file matrixOperations.cpp.

Verify Selected C++ Compiler

You can use any C++ compiler supported by MathWorks. To verify that you have a C++ compiler, type:

mex -setup cpp

This example uses the MinGW64 Compiler (C++) for C++ language compilation.

Generate Definition File

Identify the names and paths to the C++ library artifacts. By default, the function uses the name of the header file (matrixOperations) as the name of the library (libname).

productPath = fullfile(matlabroot,'extern','examples','cpp_interface');
hppFile = 'matrixOperations.hpp';
cppFile = 'matrixOperations.cpp';

Generate the definition file. MATLAB creates the definition file definematrixOperations.mlx.

clibgen.generateLibraryDefinition(fullfile(productPath,hppFile), ...
    "SupportingSourceFiles",fullfile(productPath,cppFile), ...
    "OverwriteExistingDefinitionFiles",true, ...
    "ReturnCArrays",false) % treat output as MATLAB arrays
C++ compiler set to 'MinGW64 Compiler (C++)'.
Definition file definematrixOperations.mlx contains definitions for 10 constructs supported by MATLAB.
- 5 construct(s) are fully defined.
- 5 construct(s) partially defined and commented out.

To include the 5 undefined construct(s) in the interface, uncomment and complete the definitions in definematrixOperations.mlx.
To build the interface, call build(definematrixOperations).

MATLAB creates the definition file definematrixOperations.mlx. Click the link in the output message to open the file, then continue with the next step.

Open Definition File

Open the generated definition file in Live Editor by clicking the link in the output message. Then continue with the next step.

See Also