Note that the following information is valid for MATLAB release R2014a and newer. For previous releases see:
MATLAB Compiler R2014a generated DLLs do not have the ability to enable functions calls without first initializing the MCR and the library. Addtionally, they deal primarily with mxArray data types.
To work around this, the MATLAB DLL can be wrapped by an intermediate level of code which does the following:
1. Initialize the MCR and load the MATLAB DLL (the first time it is called).
2. Convert data formats of the driver into MATLAB data (mxArray).
3. Call the MATLAB functions from the MATLAB DLL.
4. Convert MATLAB data back into data type formats that the driver can understand.
The attached file shows an example using C code.
This example was tested using MS Visual C++ 2010 Professional Edition.
Level 1 : MATLAB code
level1.m contains the MATLAB code which implements the functionality required from the MATLAB DLL. level1.m contains a MATLAB function which takes a 2D matrix and a string. The function displays the string using a dialogue box and returns the input matrix multiplied by 2.
A DLL is compiled from this MATLAB code using the following command:
NOTE: Ensure that a level1.dll from a previous run does not exist on the path before executing the following.
mcc -v -B csharedlib:level1 level1.m
This step creates level1.dll, level1.lib and level1.h among others.
Level 2 : Wrapper code
The wrapper code level2.c handles the four tasks mentioned above. When working with MSVC++, this is compiled to create level2.dll using the following command:
mbuild -v level2.c level1.lib LINKFLAGS="$LINKFLAGS /DLL /DEF:level2.def" LDEXT=".dll" CMDLINE250="mt -outputresource:$EXE';'2 -manifest $MANIFEST"
level2.def is a plain text DEF-file listing the name of the library and the functions in level2.c which need to be exported (visible) in the generated level2.dll file. Please note that level2.dll is dependent on level1.dll
If working with MinGW64, compile the DLL using:
mbuild -v -g level2.c level1.lib LDFLAGS="-shared $LDFLAGS" LDEXT=".dll"
Note that MinGW64 does not require a DEF-file.
Level 3 : Driver code
This code is indicative of the final driver code. No initialization steps are required and the functions can be called with C data types. This driver code, level3.c, is compiled using the following command:
Note that the final execution of level3.exe requires level2.dll and level1.dll to be available in the same directory as this executable or somewhere else on the system path.