Call a C Shared Library
To use one or more MATLAB® Compiler SDK™ generated C shared libraries in your C application:
Include the generated header file for each library in your application.
Each generated shared library has an associated header file named
Initialize the MATLAB Runtime proxy layer by calling
mclRunMainto call the C function in your driver code that uses the MATLAB generated shared libraries.
mclRunMain()provides a convenient cross platform mechanism for wrapping the execution of MATLAB code in the shared library.
Do not use
mclRunMain()on Mac if your application brings up its own full graphical environment.
Declare variables and process input arguments.
Initialize the MATLAB Runtime by calling the
mclInitializeApplicationfunction. This function sets up the global MATLAB Runtime state and enables the construction of MATLAB Runtime instances.
mclInitializeApplication()function once per application. It must be called before any other MATLAB API functions. You can pass application-level options to this function.
mclInitializeApplication()returns a boolean status code.
cdcommands from the driver application before calling
mclInitializeApplication. Failure to do so can cause a failure in MATLAB Runtime initialization.
For each C 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
. This function returns a Boolean status code.
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 after
Invoke functions in the library, and process the results. (This is the main body of the program.)
If your driver application displays MATLAB figure windows, include a call to
mclWaitForFiguresToDiebefore calling the
mclTerminateApplicationin the following two steps.
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 library's MATLAB Runtime instance. The library termination function is named
. Once a library has been terminated, the functions exported by the library cannot be called again in the application.
<lib>Initializecall after a
<lib>Terminatecall (whether or not the library is the same) causes unpredictable results.
When your application no longer needs to call any shared libraries, call the
This function frees application-level resources used by the MATLAB Runtime. Once you call this function, no further calls can be made to shared libraries in the application.
Clean up variables, close files, and exit.
The following example from
matrix.c illustrates all of the above steps.
Restrictions When Using MATLAB Function
You cannot use the MATLAB function
loadlibrary in MATLAB to load a C shared library built with MATLAB
For more information about using
loadlibrary, see Calling Shared Libraries in Deployed Applications.