MATLAB: How to pass strings into a C shared library built using MATLAB Compiler 4.5 (R2006b)

MATLAB Compiler

I would like an example showing how to pass string arguments into a MATLAB Compiler-generated C shared library, and receive string results from the library.

Best Answer

This example passes string variables into and out of a C shared library built with MATLAB Compiler 4.5 (R2006b). It is based on the C shared library example in the documentation
web([docroot,'/toolbox/compiler/f2-1000836.html'])
1. Building the shared library
Download the following attached files your work directory, using the links at the bottom of this page:
revstr.m
revdriver.c
Note: revdriver.c contains the driver application's main function.
2. Compiling the library
First, choose your C/C++ compiler:
mbuild -setup
To create the shared library, enter the following command:
mcc -B csharedlib:librev revstr.m -v
The arguments to the MCC command are explained in the C shared library example.
3. Compiling the driver application
To compile the driver code, revdriver.c using the selected C/C++ compiler, execute the following platform-specific MBUILD command
mbuild revdriver.c librev.lib (Windows)
mbuild revdriver.c -L. -lrev -I. (UNIX)
This generates a driver application, revdriver.exe, on Windows, and revdriver, on UNIX.
4. Running the driver application
To execute the standalone application, add the directory containing the shared library that was created in step 2 in Building the Shared Library to your dynamic library path. Update the path for your platform by following the instructions in the section 'Developing and Testing Components on a Development Machine' of the C shared library example.
Execute the driver application from the appropriate command prompt (DOS prompt on Windows, shell prompt on UNIX) by typing the application name:
revdriver.exe (On Windows)
revdriver (On UNIX)
The results are displayed as:
The value of the original string is:
abcdef
The value of the reversed string is:
fedcba