Main Content

Publish Interface to Shared C++ Library on Linux

This example creates a MATLAB® interface to a C++ library matrixOperations for Linux®. The process includes:

  • Generate a definition file (definematrixlib.mlx).

  • Modify the definition file to complete any definitions that MATLAB is not able to automatically convert.

  • Build the library interface.

For more information, see Build MATLAB Interface to C++ Library.

MATLAB provides a C++ library and header files for you to use in this example. The files are in this folder and its subfolders:


Generate Definition File

The first step for generating the interface is preparing the necessary folders and files and calling clibgen.generateLibraryDefinition to generate the definition file.

Verify Supported C++ Compiler

To build the interface, use the same compiler that was used to build the C++ library.

mex -setup cpp

Create Publisher Folder

Create a folder for the MATLAB interface file.

pubPath = "~/MATLAB/publisher/matrixexample/";
if ~isfolder(pubPath)

Identify C++ Library Files

Identify the names and paths to C++ library artifacts.

productPath = fullfile(matlabroot,'extern','examples','cpp_interface');
libPath = fullfile(productPath,'glnxa64');
% Header file name
hppFile = 'matrixOperations.hpp';
% Full path to folder containing all header files
hppPath = productPath;
% Full path to folder containing include files
iPath = hppPath;
% Library file name
libFile = '';

Name the Interface

By default, MATLAB creates an interface called matrixOperations. For this example, change the name to matrixlib.

libname = "matrixlib";

Call clibgen.generateLibraryDefinition

To create the interface, you must specify:

  • Header file name matrixOperations.hpp and its location.

  • Path to the folder containing the include files, using the 'IncludePath' argument.

  • Name and location of the library file matrixOperations.lib, using the 'Libraries' argument.

Optionally, you can:

  • Rename the library, using the 'PackageName' argument.

  • Display generation messages, using the 'Verbose' argument.

'IncludePath', iPath,... 
'Libraries', fullfile(libPath,libFile),... 
'PackageName', libname,...
'ReturnCArrays',false,... % treat output as MATLAB arrays

Validate the library.


View Functionality

Although some constructs require additional definition, you can view the available functionality. If this functionality is sufficient for your needs, then you can continue with the Build Library Interface step. Otherwise, continue with the step Define Missing Constructs.

MATLAB Interface to matrixlib Library

Class clib.matrixlib.Mat


    uint64 getLength()

  No Properties defined


Define Missing Constructs

To define the missing constructs, click the link in the generateLibraryDefinition output message to edit the definitions in definematrixlib.mlx. For information about editing this file and examples for specifying arguments, see Define Missing Information for MATLAB Signatures.

  1. Search the definition file for the setMat method and uncomment the statements defining it. To define the src argument, in this defineArgument statement, replace <SHAPE> with "len".

    defineArgument(setMatDefinition, "src", "clib.array.matrixlib.Int", "input", "len");
  2. In the method getMat, define the RetVal output by replacing <SHAPE> with "len".

    defineOutput(getMatDefinition, "RetVal", "int32", "len");
  3. In the method copyMat, define the dest argument by replacing <SHAPE> with "len".

    defineArgument(copyMatDefinition, "dest", "clib.array.matrixlib.Int", "input", "len");
  4. In the function addMat, define the mat argument in function addMat by replacing <SHAPE> with 1.

    defineArgument(addMatDefinition, "mat", "clib.matrixlib.Mat", "input", 1);
  5. In the function updateMatBySize, define the arr argument by replacing <SHAPE> with "len".

    defineArgument(updateMatBySizeDefinition, "arr", "clib.array.matrixlib.Int", "input", "len");

Save and close the definition file.

Build Library Interface

Create the MATLAB interface file matrixlibInterface.dll.

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

Be sure to click the link in the message to add the interface file to the path.

Test the Interface

To test the interface, see Call Functions in Linux Interface to C++ Shared Library.

See Also

Related Topics