Main Content

Use Multiple Shared Libraries in Single Application

When developing applications that use multiple MATLAB® shared libraries, consider the following:

  • Each MATLAB shared library must be initialized separately.

  • Each MATLAB shared library must be terminated separately.

  • MATLAB function handles cannot be shared between shared libraries.

  • MATLAB figure handles cannot be shared between shared libraries.

  • MATLAB objects cannot be shared between shared libraries.

  • C, Java®, and .NET objects cannot be shared between shared libraries.

  • Executable data stored in cell arrays and structures cannot be shared between shared libraries

Initialize and Terminate Multiple Shared Libraries

To initialize and terminate multiple shared libraries:

  1. Initialize the MATLAB Runtime using mclmcrInitialize().

  2. Call the portion of the application that executes the MATLAB code using mclRunMain().

  3. Before initializing the shared libraries, initialize the MATLAB application state using mclInitializeApplication().

  4. For each MATLAB shared library, call the generated initialization function, libraryInitialize().

  5. Add the code for working with the MATLAB code.

  6. For each MATLAB shared library, release the resources used by the library using the generated termination function, libraryTerminate().

  7. Release the resources used by the MATLAB Runtime by calling mclTerminateApplication().

This example shows the use of two shared libraries.

 Example driver code

Work with MATLAB Function Handles

A MATLAB function handle can be passed back and forth between a MATLAB Runtime instance and an application. However, it cannot be passed from one MATLAB Runtime instance to another. For example, suppose that you had two MATLAB functions, get_plot_handle and plot_xy, and plot_xy used the function handle created by get_plot_handle.

% Saved as get_plot_handle.m
function h = get_plot_handle(lnSpec, lnWidth, mkEdge, mkFace, mkSize)
h = @draw_plot;
    function draw_plot(x, y)
        plot(x, y, lnSpec, ...
            'LineWidth', lnWidth, ...
            'MarkerEdgeColor', mkEdge, ...
            'MarkerFaceColor', mkFace, ...
            'MarkerSize', mkSize)
% Saved as plot_xy.m
function plot_xy(x, y, h)
h(x, y);

If you packaged them into two separate shared libraries, the call to plot_xy would throw an exception.

 Example driver code

One way to handle the situation is to package both functions into a single shared library. For example, if you called the shared library plot_functions, your application would only need one call to initialize the function and you could pass the function handle for plot_xy without error.

 Example driver code

Work with Objects

MATLAB Compiler SDK™ enables you to return the following types of objects from the MATLAB Runtime to your application code:


  • C++

  • .NET

  • Java

  • Python®

However, you cannot pass an object created in one MATLAB Runtime instance into a different MATLAB Runtime instance. This conflict can happen when a function that returns an object and a function that manipulates that object are packaged into different shared libraries.

For example, say that you develop two functions. The first creates a bank account for a customer. The second transfers funds between two accounts.

% Saved as account.m
classdef account < handle

    properties (SetAccess = protected)
        balance = 0
        function obj = account(name)
   = name;
            obj.number = round(rand * 1000);
        function deposit(obj, deposit)
            new_bal = obj.balance + deposit;
            obj.balance = new_bal;
        function withdraw(obj, withdrawl)
            new_bal = obj.balance - withdrawl;
            obj.balance = new_bal;
% Saved as open_acct .m
function acct = open_acct(name, open_bal )

    acct = account(name);

    if open_bal > 0
% Saved as transfer.m
function transfer(source, dest, amount)

    if (source.balance > amount)


If you packaged open_acct.m and transfer.m into separate shared libraries, you could not transfer funds using accounts created with open_acct. The call to transfer would throw an exception. One way of resolving this is to package both functions into a single shared library. You could also refactor the application so as not to pass MATLAB objects to the functions.

Related Topics