Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Compiler   

Using C/C++ Shared Libraries on a Mac OS X System

To use a MATLAB Compiler generated library on a Mac OS X system, you must create a separate thread that initializes the shared library and call that library's functions. The main thread of the application is the thread that calls your driver program's main() function. The body of your main() function must create a new thread, passing to it the address of a thread-function containing the library initialization and necessary calls to the shared library generated by MATLAB Compiler. The new thread does the main work of the application, including calling MATLAB Compiler generated libraries.

In past releases, it was necessary to create and execute a CFRunLoop in the main thread, as well as to call mclSetExitCode. Now, however, the same functionality can be accomplished with a single call to mclRunMain.

The following example illustrates this procedure. This example rewrites the C shared library example from this chapter for use on Mac OS X. Follow the same procedure as in the earlier example to build and run this application.

/*=======================================================
 *
 * MATRIXDRIVER.C	Sample driver code that calls the shared
 *	        library created using MATLAB Compiler. Refer to the 
 *          documentation of MATLAB Compiler for more info 
 *          on this
 *
 * This is the wrapper C code to call a shared library created 
 * using MATLAB Compiler.
 *
 * Copyright 1984-2005 The MathWorks, Inc.
 *
 *=======================================================*/

#include <stdio.h>

#ifdef __APPLE_CC__
#include <CoreFoundation/CoreFoundation.h>
#endif

/* Include the MCR header file and the library specific header
 * file as generated by MATLAB Compiler */
#include "libmatrix.h"

/* This function displays double matrix stored in mxArray */
void display(const mxArray* in);

void *run_main(void *x)
{
  int *err = x;
    mxArray *in1, *in2; /* Define input parameters */
    mxArray *out = NULL;/* and output parameters to be 
                         * passed to lib functions */
    
    double data[] = {1,2,3,4,5,6,7,8,9};

    /* Call the mclInitializeApplication routine. Make sure that
     * the application was initialized properly by checking the 
     * return status. This initialization has to be done before 
     * calling any MATLAB API's or MATLAB Compiler generated
     * shared library functions.  */
    if( !mclInitializeApplication(NULL,0) )
    {
        fprintf(stderr, "Could not initialize application.\n");
	*err = -1;
        return(x);
    }
    
    /* Create the input data */
    in1 = mxCreateDoubleMatrix(3,3,mxREAL);
    in2 = mxCreateDoubleMatrix(3,3,mxREAL);
    memcpy(mxGetPr(in1), data, 9*sizeof(double));
    memcpy(mxGetPr(in2), data, 9*sizeof(double));
    
    /* Call the library intialization routine and make sure that
     * the library was initialized properly. */
    if (!libmatrixInitialize()){
        fprintf(stderr,"Could not initialize the library.\n");
        *err = -2;
    }
    else
    {
        /* Call the library function */
        mlfAddmatrix(1, &out, in1, in2);
	/* Display the return value of the library function */
	printf("The value of added matrix is:\n");
	display(out);
	/* Destroy the return value since this variable will be reused
	 * in the next function call. Since we are going to reuse the
	 * variable, we have to set it to NULL. Refer to 
	 * documentation for more information on this. */
	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;
	
	/* Call the library termination routine */
	libmatrixTerminate();
	
	/* Free the memory created */
	mxDestroyArray(in1); in1=0;
	mxDestroyArray(in2); in2 = 0;
    }
/* On MAC, you need to call mclRunMain with the appropriate
* exit status. Also, you should call mclmcrInitialize
* application before you call mclRunMain.
* mclTerminateApplication terminates the entire application. */
mclTerminateApplication();
return 0
}
int main()
{
mclmcrInitialize();
return mclRunMain((mclmainFcn)run_main,0,NULL);}


/*DISPLAY This function will display the double matrix 
 * stored in an mxArray. This function assumes that 
 * the mxArray passed as input contains double array.
 */
void display(const mxArray* in)
{
    int i=0, j=0; /* loop index variables */
    int r=0, c=0; /* variables to store the row 
                   * and column length of the matrix */
    double *data; /* variable to point to the double data stored
                   * within the mxArray */

    /* Get the size of the matrix */
    r = mxGetM(in);
    c = mxGetN(in);
    /* Get a pointer to the double data in mxArray */
    data = mxGetPr(in);
    
    /* Loop through data and display in matrix format */
        for( i = 0; i < c; i++ ){
        for( j = 0; j < r; j++){
            printf("%4.2f\t",data[j*c+i]);
        }
        printf("\n");
    }
    printf("\n");
}

The Mac version of the matrixdriver application differs from the version on other platforms in these significant ways:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS