MATLAB: What libraries should I link against when creating a managed application which calls a MATLAB Compiler 4.5 (R2006b) generated shared library

MATLAB Compilerunmanaged

I have created a shared library with MATLAB Compiler. I am calling this library from C#. In order to create the mxArrays which need to be passed to these functions, I have imported functions from libmx.dll such as mxCreateDoubleMatrix as follows:
[DllImport("libmx.dll")]
private static extern IntPtr mxCreateDoubleMatrix(int m, int n, Complexity complexity);
When I run my application I receive segmentation violations.

Best Answer

The only MathWorks libraries which you should link against when calling a C/C++ shared library are the appropriate mclmcrrt library for your platform and version and the shared library which was generated by MATLAB Compiler. This is because all access to the MATLAB Compiler Runtime (MCR) (including the creation and access of mxArrays) must be marshaled through the mclmcrrt library for thread safety reasons.
For example on 32-bit Windows for MATLAB Compiler 4.6 (R2007a) this means linking against mwmclmcrrt76.dll. If you are implicitly linking (using load time dynamic linking) this means linking against the import library mclmcrrt.lib.
On a 32-bit Linux platform for MATLAB Compiler 4.6 (R2007a) this means linking against libmwmcrmcrrtso.7.6.
For explicit linking (run-time dynamic linking) the situation is more complicated because of a compatibility layer introduced in the mxArray API in MATLAB 7.3 (R2006b) for support of large arrays. The changes to the API required creation of two sets of functions *_700 and *_730 (for example mxCreateDoubleMatrix_700 and mxCreateDoubleMatrix_730). In general, you should use the *_730 functions for all applications which call MATLAB Compiler generated shared libraries. The *_700 functions are for backward compatibility.
In the previous example, where explicit linking is used from C#, with MATLAB Compiler 4.6 (R2007a) the import statement should read
[DllImport("mclmcrrt76.dll")]
private static extern IntPtr mxCreateDoubleMatrix_730([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);