Call a Shared Library

To use a MATLAB® Compiler SDK™ generated shared library in your application:

  1. Include the generated header file for each library in your application.

    Each generated shared library has an associated header file named libname.h.

  2. Initialize the MATLAB Runtime proxy layer by calling mclmcrInitialize().

  3. Use mclRunMain() to call the C function where your MATLAB functions are used.

    mclRunMain() provides a convenient cross platform mechanism for wrapping the execution of MATLAB code.

      Caution   Do not use mclRunMain() if your application brings up its own full graphical environment.

  4. Initialize the MATLAB Runtime and set the global settings by calling mclInitializeApplication() API function.

    Call the mclInitializeApplication() function once per application, and it must be called before calling any other MATLAB API functions. You may pass in application-level options to this function. mclInitializeApplication() returns a Boolean status code.

  5. For each MATLAB Compiler SDK generated shared library that you include in your application, call the initialization function for the library.

    The initialization function performs library-local initialization. It unpacks the deployable archive and starts a MATLAB Runtime instance with the necessary information to execute the code in that archive. The library initialization function is named libnameInitialize(). This function returns a Boolean status code.

      Note   On Windows®, if you want to have your shared library call a MATLAB shared library, the MATLAB library initialization function (e.g., <libname>Initialize, <libname>Terminate, mclInitialize, mclTerminate) cannot be called from your shared library during the DllMain(DLL_ATTACH_PROCESS) call. This applies whether the intermediate shared library is implicitly or explicitly loaded. Place the call somewhere after DllMain().

  6. Call the exported functions of each library as needed.

  7. When your application no longer needs a given library, call the termination function for the library.

    The terminate function frees the resources associated with the libraries MATLAB Runtime instance. The library termination function is named libnameTerminate(). Once a library has been terminated, the functions exported by the library cannot be called again in the application.

  8. When your application no longer needs to call any MATLAB Compiler SDK generated libraries, call the mclTerminateApplication API function.

    This function frees application-level resources used by the MATLAB Runtime. Once you call this function, no further calls can be made to MATLAB Compiler SDK generated libraries in the application.

The following code example is from matrixdriver.c:

#include "libmatrix.h"

int run_main(int argc, char **argv)
{
    mxArray *in1, *in2;
    mxArray *out = NULL;
    
    double data[] = {1,2,3,4,5,6,7,8,9};

    if( !mclInitializeApplication(NULL,0) )
    {
        fprintf(stderr, "Could not initialize the application.\n");
    	return -1;
    }
    
 
   in1 = mxCreateDoubleMatrix(3,3,mxREAL);
    in2 = mxCreateDoubleMatrix(3,3,mxREAL);
    memcpy(mxGetPr(in1), data, 9*sizeof(double));
    memcpy(mxGetPr(in2), data, 9*sizeof(double));
    
    if (!libmatrixInitialize()){
        fprintf(stderr,"Could not initialize the library.\n");
        return -2;
    }
    else
    {
        mlfAddmatrix(1, &out, in1, in2);
        printf("The value of added matrix is:\n");
        display(out);

        mxDestroyArray(out); out=0;
        mlfMultiplymatrix(1, &out, in1, in2);
        printf("The value of the multiplied matrix is:\n");
        display(out);
        mxDestroyArray(out); out=0;
        mlfEigmatrix(1, &out, in1);
        printf("The eigenvalues of the first matrix are:\n");
        display(out);
        mxDestroyArray(out); out=0;
        
        libmatrixTerminate();
        
        mxDestroyArray(in1); in1=0;
        mxDestroyArray(in2); in2 = 0;
    }

    mclTerminateApplication();
    return 0;
}

int main()
{
    mclmcrInitialize();
    return mclRunMain((mclMainFcnType)run_main,0,NULL);
}

Restrictions When Using MATLAB Function loadlibrary

You cannot use the MATLAB function loadlibrary inside of MATLAB to load a C shared library built with MATLAB Compiler SDK.

For more information about using loadlibrary, see MATLAB Libraries Using loadlibrary.

Was this topic helpful?