Main Content

Integrate C++ Shared Libraries with MATLAB Data API

Workflow to Create C++ Shared Library Using Generic Interface

To create a C++ shared library that uses the MATLAB® Data API:

  1. Package your MATLAB code into an archive (.ctf) file using the Library Compiler app or the function. Select the MATLAB Data interface.

  2. Write C++ code using the generic interface.

  3. Link the driver code against header files provided with MATLAB Runtime.

  4. Run your application.

For an example of this workflow, see Generate a C++ MATLAB Data API Shared Library and Build a C++ Application.

Write C++ Code Using Generic Interface

The basic workflow for using the generic interface for C++ shared libraries is as follows:

  • Call the free function initMATLABApplication, which optionally takes a vector of run time options such as -nojvm and -logfile. The function returns a shared_ptr.

  • Initialize a matlab::data::ArrayFactory, which you use to produce matlab::data::Array objects that you pass into function calls.

  • For each library that you initialize, call initMATLABLibrary, which takes two parameters:

    • Copy of the shared_ptr that was returned by initMATLABApplication

    • Path to the archive (.ctf file)

  • To call a function in an initialized library, call feval or fevalAsync on the unique_ptr that was returned by initMATLABLibrary. There are several overloaded versions of each. They all take the name of the MATLAB function as the first parameter. However, these differ in terms of whether they accept and return single matlab::data::Array objects, arrays of matlab::data::Array, or native types. The forms that return a native type must take the type as a template parameter.

  • To terminate a library, either call reset on its unique_ptr, or allow it to go out of scope.

  • To terminate the application, either call reset on its shared_ptr, or allow it to go out of scope. It does not terminate until all the libraries created underneath it have been terminated or gone out of scope.

For example code that uses the C++ MATLAB Data Array API, see matrix_mda.cpp located in matlabroot\extern\examples\compilersdk\c_cpp\matrix.


Related Topics