Limitations to Shared Library Support

MATLAB Supports C Library Routines

The MATLAB® shared library interface supports C library routines only. Most professionally written libraries designed to be used by multiple languages and platforms work fine. Many homegrown libraries or libraries that have only been tested from C++ have interfaces that are not usable and require modification or an interface layer. In this case, we recommend using MEX-files.

Workarounds for Loading C++ Libraries

The shared library interface does not support C++ classes or overloaded functions elements. However, you can apply one of the following methods to load a C++ library using loadlibrary.

Declare Functions as extern "C"

For example, the following function prototype from the file shrlibsample.h shows the syntax to use for each function:

#ifdef __cplusplus
extern "C" {
void addMixedTypes(
  short  x,
  int    y,
  double z

/* other prototypes may be here */

#ifdef __cplusplus

The following C++ code is not legal C code for the header file:

extern "C" void addMixedTypes(short x,int y,double z); 

Add Module Definition File in Visual Studio

While building the DLL from C++ code in Microsoft® Visual Studio®, add a Module Definition File (.DEF) in the project. At a minimum, the DEF file must contain the following module-definition statements:

  • The first statement in the file must be the LIBRARY statement.

  • The EXPORTS statement lists the names and, optionally, the ordinal values of the functions exported by the DLL.

For example, if a DLL exports functions multDoubleArray and addMixedTypes, module.def contains:


Using Bit Fields

You can modify a bit field declaration by using type int or an equivalent. For example, if your library has the following declared in its header file:

int myfunction();

struct mystructure
    /* note the sum of fields bits */
    unsigned field1 :4;
    unsigned field2 :4;

you can replace it with:

int myfunction();

struct mystructure
    /* field 8 bits wide to be manipulated in MATLAB */
    /* A char is 8 bits on all supported platforms */
    char allfields; 

It is then possible to access the data in the two fields using bit masking in MATLAB.

Using Enum Declarations

char definitions for enum are not supported. In C, a char constant, for example 'A', is automatically converted to its numeric equivalent (65). MATLAB does not convert constants. To use this type of enum, modify the header file by replacing 'A' with the number 65 (int8(‘A') == 65). For example, replace:

enum Enum1 {ValA='A',ValB='B'};


enum Enum1 {ValA=65,ValB=66};

Unions Not Supported

Unions are not supported. It might be possible to modify the source code taking out the union declaration and replacing it with the largest alternative, then writing MATLAB code to interpret the results as needed. For example, replace the following union:

struct mystruct
       struct {char byte1,byte2;};
	       short word;


struct mystruct 
    short word;

where on a little-endian based machine, byte1 is mod(f,256), byte2 is f/256, and word=byte2*256+byte1.

Compiler Dependencies

Header files must be compatible with the supported compilers on a platform. For an up-to-date list of supported compilers, see the Supported and Compatible Compilers website. You cannot load external libraries with explicit dependencies on other compilers.

Limitations Using Structures

MATLAB returns pointers to structures. Return by value is not supported.

Nested structures or structures containing a pointer to a structure are not supported. However, MATLAB can access an array of structures created in an external library.

Limitations Using Pointers

Function Pointers

The shared library interface does not support library functions that work with function pointers.

Multilevel Pointers

Limited support for multilevel pointers and structures containing pointers. Using inputs and outputs and structure members declared with more than two levels of indirection is unsupported. For example, double ***outp translated to doublePtrPtrPtr is not supported.

Functions with Variable Number of Input Arguments Not Supported

The shared library interface does not support library functions with variable number of arguments, represented by an ellipsis (...).

You can create multiple alias functions in a prototype file, one for each set of arguments used to call the function. For more information, see MATLAB Prototype Files.

Was this topic helpful?