MATLAB Compiler Previous page   Next Page

C Shared Library Example

This example takes several M-files and creates a C shared library. It also includes a stand-alone driver application to call the shared library.

Building the Shared Library

  1. Copy the following files from <matlabroot>/extern/examples/compiler to your work directory.

  1. To create the shared library, use
  1. The -B csharedlib option is a bundle option that expands into

    The -W lib:<libname> option tells the MATLAB Compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library. Note the directory where the Compiler puts the shared library because you will need it later on.

Writing the Driver Application

All programs that call MATLAB Compiler-generated shared libraries have roughly the same structure:

  1. Declare variables and process/validate input arguments.
  2. Call mclInitializeApplication, and test for success. This function sets up the global MCR state and enables the construction of MCR instances.
  3. Call, once for each library, <libraryname>Initialize, to create the MCR instance required by the library.
  4. Invoke functions in the library, and process the results. (This is the main body of the program.)

  1. Call, once for each library, <libraryname>Terminate, to destroy the associated MCR.
  2. Call mclTerminateApplication to free resources associated with the global MCR state.
  3. Clean up variables, close files, etc., and exit.

This example uses matrixdriver.c as the driver application.

Compiling the Driver Application

To compile the driver code, matrixdriver.c, you use your C/C++ compiler. Execute the following mbuild command that corresponds to your development platform. This command uses your C/C++ compiler to compile the code.

This generates a stand-alone application, matrixdriver.exe, on Windows, and matrixdriver, on UNIX.

Difference in the Exported Function Signature.   The interface to the mlf functions generated by the Compiler from your M-file routines has changed from earlier versions of the Compiler. The generic signature of the exported mlf functions is

Refer to the header file generated for your library for the exact signature of the exported function. For example, in the library created in the previous section, the signature of the exported addmatrix function is

Testing the Driver Application

These steps test your stand-alone driver application and shared library on your development machine.

  1. To run the stand-alone application, add the directory containing the shared library that was created in step 2 in Building the Shared Library to your dynamic library path.
  2. Update the path for your platform by following the instructions in Testing Components on Development Machine.
  3. Run the driver application from the prompt (DOS prompt on Windows, shell prompt on UNIX) by typing the application name.
  1. The results are displayed as:

Deploying Stand-Alone Applications That Call MATLAB Compiler-Based Shared Libraries

Gather and package the following files and distribute them to the deployment machine.

Component
Description
MCRInstaller.zip
(UNIX) MATLAB Component Runtime library archive; platform-dependent file that must correspond to the end-user's platform
MCRInstaller.exe
(Windows) Self-extracting MATLAB Component Runtime library utility; platform-dependent file that must correspond to the end-user's platform
unzip
(UNIX) Utility to unzip MCRInstaller.zip (optional). The target machine must have an unzip utility installed.
matrixdriver
Application; matrixdriver.exe for Windows
libmatrix
Shared library; extension varies by platform, for example, DLL on Windows

Deploying Shared Libraries to be Used with Other Projects

To distribute the shared library for use with an external application, you need to distribute the following.

Component
Description
MCRInstaller.zip
(UNIX) MATLAB Component Runtime library archive; platform-dependent file that must correspond to the end-user's platform
MCRInstaller.exe
(Windows) Self-extracting MATLAB Component Runtime library utility; platform-dependent file that must correspond to the end-user's platform
unzip
(UNIX) Utility to unzip MCRInstaller.zip (optional). The target machine must have an unzip utility installed.
libmatrix.ctf
Component Technology File archive; platform-dependent file that must correspond to the end-user's platform
libmatrix
Shared library; extension varies by platform, for example, DLL on Windows
libmatrix.h
Library header file


Previous page  C Shared Library Target Calling a Shared Library Next page

© 1994-2005 The MathWorks, Inc.