I want to build with Matlab Compiler SDK a DLL that can be called from LabView. As you may know, the library generated by the Matlab Compiler SDK (lets call it 'my_dll.dll'/'my_dll.lib') is not directly callable by LabView, as it uses non-standard data types ( mxArrays ).
Thus, my plan is to generate in C a wrapper code. This wrapper code, whenever it is called, will execute the needed routines of the Matlab Runtime Engine and call the functions generated in 'my_dll.dll'. This wrapper code would be basically extremely similar to any other executable or application using the previously generated DLL, following the guidelines of this.
Code example:
int main(double *input){
/* Call the mclInitializeApplication routine. Make sure that the application
* was initialized properly by checking the return status. This initialization
* has to be done before calling any MATLAB API's or MATLAB Compiler SDK generated
* shared library functions. */
mclmcrInitialize();
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
return -1;
}
return mclRunMain((mclMainFcnType)wrapper_main,0,NULL);
}
double wrapper_main(int argc, char **argv) {
//declare variables
double *out; // Here my output will be stored
double number = 16; // This is the input number
// Initialise library
dll_layer1Initialize();
//Create two pointers of mxArray type to store inputs and outputs
mxArray *in1_ptr;
mxArray *out1_ptr = NULL;
//Allocate input pointer to a 1 by 1 double, real matrix
in1_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);
//Move the data from the input to the pointer
memcpy(mxGetPr(in1_ptr), &number, 1 * sizeof(double));
//Pass values to mlfFoo and receive in mxArray type variable
//mlfFoo is my matlab function. In this case it only performs
//the square root of the input number.
mlfFoo(1, &out1_ptr, in1_ptr);
out = mxGetPr(out1_ptr);
printf("\n Result is: %f", *out);
//Terminate foo implementation
dll_layer1Terminate();
mclTerminateApplication();
return 0;
}
However, in order to call this C wrapper code from LabView, I would have to generate a new DLL, and here comes the problem, since I do not find the way to do this. The following picture summarizes what I want to do.
Things that I have tried:
1 - Compile the C wrapper code and my_dll.lib to generate a wrapper.dll with the Matlab Compiler SDK 'mbuild' command. This only creates an executable, but not a dll. Maybe I am missing something.
2 - Use Visual Studio to create the DLL. The problem I am facing here is that when I am going to build the project it gives me plenty of errors, like:
Error LNK2019 unresolved external symbol _mclRunMain_proxy referenced in function "public: static double __cdecl Wrapper::Functions::wrapper_sqrt(double)" (?wrapper_sqrt@Functions@Wrapper@@SANN@Z) Win32Project1 ...\visual studio 2015\Projects\Win32Project1\Win32Project1\wrapper.obj 1
Maybe my error here is that I am not properly linking the Matlab libraries, in which case I do not know how to fix it.
Other possibilities to run Matlab code in LabView is the use of MathScript node, Matlab Script node or Matlab Coder(for creating also a DLL) however, I am discarding those options for several other reasons.
Thank you beforehand for your help.
