Main Content

Integrate C++ Shared Libraries with mwArray

C++ Shared Library Wrapper

When you create a C++ shared library using MATLAB® Compiler SDK™, the compiler generates a wrapper file and a header file. The header file contains all of the entry points for the compiled MATLAB functions.

C++ libmatrix Example

The examples on this page reference the C++ shared library libmatrix. To create this library, complete the example Generate a C++ mwArray API Shared Library and Build a C++ Application.

Ensure both files are in the current folder, then compile the matrix_mwarray.cpp application using mbuild.

mbuild matrix_mwarray.cpp libmatrix.lib

Note

Due to name mangling in C++, you must compile your C++ application with the same version of the third-party compiler that you use to compile your C++ shared library.

Arrays in C++ Applications

In the matrix_mwarray.cpp code, arrays are represented by objects of the class mwArray. Every mwArray class object contains a pointer to a MATLAB array structure. For this reason, the attributes of an mwArray object are a superset of the attributes of a MATLAB array. Every MATLAB array contains information about the size and shape of the array (i.e., the number of rows, columns, and pages) and either one or two arrays of data. The first array stores the real part of the array data and the second array stores the imaginary part. For arrays with no imaginary part, the second array is not present. The data in the array is arranged in column-major, rather than row-major, order.

For information about how MATLAB Compiler SDK uses a proxy layer for the linked libraries, see Understand the mclmcrrt Proxy Layer.

Incorporate C++ Shared Library into Application

There are two main differences to note when using a C++ shared library:

  • Interface functions use the mwArray type to pass arguments, rather than the mxArray type used with C shared libraries.

  • C++ exceptions are used to report errors to the caller. Therefore, all calls must be wrapped in a try-catch block.

Exported Function Signature

The C++ shared library target generates two sets of interfaces for each MATLAB function. For more information, see Functions Generated from MATLAB Files. The generic signature of the exported C++ functions is as follows:

MATLAB Functions with No Return Values

bool MW_CALL_CONV <function-name>(<const_mwArray_references>); 

MATLAB Functions with at Least One Return Value

bool MW_CALL_CONV <function-name>(int <number_of_return_values>,
     <mwArray_references>, <const_mwArray_references>);

In this case, const_mwArray_references represents a comma-separated list of references of type const mwArray& and mwArray_references represents a comma-separated list of references of type mwArray&. For example, in the libmatrix library, the C++ interface to the addmatrix MATLAB function is generated as:

void addmatrix(int nargout, mwArray& a, const mwArray& a1,  
               const mwArray& a2);

where a is an output parameter and a1 and a2 are input parameters.

Input arguments passed to the MATLAB function via varargin must be passed via a single mwArray that is a cell array. Each element in the cell array must constitute an input argument. Output arguments retrieved from the MATLAB function via varargout must be retrieved via a single mwArray that is a cell array. Each element in the cell array will constitute an output argument. The number of elements in the cell array will be equal to number_of_return_values - the number of named output parameters. Also note that,

  • If the MATLAB function takes a varargin argument, the C++ function must be passed an mwArray corresponding to that varargin, even if the mwArray is empty.

  • If the MATLAB function takes a varargout argument, the C++ function must be passed an mwArray corresponding to that varargin, even if number_of_return_values is set to the number of named output arguments, which means meaning that varargout will be empty.

  • The varargout argument needs to follow any named output arguments and precede any input arguments.

  • The varargin argument needs to be the last argument.

Error Handling

C++ interface functions handle errors during execution by throwing a C++ exception. Use the mwException class for this purpose. Your application can catch mwExceptions and query the what() method to get the error message. To correctly handle errors when calling the C++ interface functions, wrap each call inside a try-catch block.

	try
{
		...
		(call function)
		...
}
catch (const mwException& e)
{
		...
		(handle error)
		...
}

The matrix_mwarray.cpp application illustrates the typical way to handle errors when calling the C++ interface functions.

Working with C++ Shared Libraries and Sparse Arrays

The MATLAB Compiler SDK C/C++ API includes static factory methods for working with sparse arrays.

For a complete list of the methods, see C++ Utility Classes.

Related Topics