MATLAB Compiler Previous page   Next Page

Calling a Shared Library

At run-time, there is an MCR instance associated with each individual shared library. Consequently, if an application links against two MATLAB Compiler-generated shared libraries, there will be two MCR instances created at run-time.

You can control the behavior of each MCR instance by using MCR options. The two classes of MCR options are global and local. Global MCR options are identical for each MCR instance in an application. Local MCR options may differ for MCR instances.

To use a shared library, you must use these functions:

mclInitializeApplication allows you to set the global MCR options. They apply equally to all MCR instances. You must set these options before creating your first MCR instance.

These functions are necessary because some MCR options such as whether or not to start Java, the location of the MCR itself, whether or not to use the MATLAB JIT feature, and so on, are set when the first MCR instance starts and cannot be changed by subsequent instances of the MCR.

Function Signatures

The function signatures are

mclInitializeApplication.   Takes an array of strings of user-settable options (these are the very same options that can be provided to mcc via the -R option) and a count of the number of options (the length of the option array). Returns true for success and false for failure.

mclTerminateApplication.   Takes no arguments and can only be called after all MCR instances have been destroyed. Returns true for success and false for failure.

This C example shows typical usage of the functions.

Steps to Use a Shared Library

To use a MATLAB Compiler-generated shared library in your application, you must perform the following steps:

  1. Include the generated header file for each library in your application. Each MATLAB Compiler-generated shared library has an associated header file named <libname>.h, where <libname> is the library's name that was passed in on the command line when the library was compiled.
  2. Initialize the MATLAB libraries by calling the mclInitializeApplication API function. You must call this function once per application, and it must be called before calling any other MATLAB API functions, such as C MX-functions or C MAT-file functions. mclInitializeApplication must be called before calling any functions in a MATLAB Compiler-generated shared library. You may optionally pass in application-level options to this function. mclInitializeApplication returns a Boolean status code. A return value of true indicates successful initialization, and false indicates failure.
  3. For each MATLAB Compiler-generated shared library that you include in your application, call the library's initialization function. This function performs several library-local initializations, such as unpacking the CTF archive, and starting an MCR instance with the necessary information to execute the code in that archive. The library initialization function will be named <libname>Initialize(), where <libname> is the library's name that was passed in on the command line when the library was compiled. This function returns a Boolean status code. A return value of true indicates successful initialization, and false indicates failure.

  1. Call the exported functions of each library as needed. Use the C MX API to process input and output arguments for these functions.
  2. When your application no longer needs a given library, call the library's termination function. This function frees the resources associated with its MCR instance. The library termination function will be named <libname>Terminate(), where <libname> is the library's name that was passed in on the command line when the library was compiled. Once a library has been terminated, that library's exported functions should not be called again in the application.
  3. When your application no longer needs to call any MATLAB Compiler-generated libraries, call the mclTerminateApplication API function. This function frees application-level resources used by the MCR. Once you call this function, no further calls can be made to MATLAB Compiler-generated libraries in the application.

Previous page  C Shared Library Example C++ Shared Library Target Next page

© 1994-2005 The MathWorks, Inc.