MATLAB: Can I use a dll library generated by MATLAB Coder in a separate cpp file using Microsoft Visual Studio Project

matlab coder

I have used Matlab Coder to generate DLL-files of the "umbrella function" myFun.m, which is an entry function which calls another entry function, mySubFun.m.
Now I want to let a huge, external cpp-solution call the cpp-generated version of mySubFun() without using mySubFun.cpp, instead by using myFun.dll.
How do I approach this?

Best Answer

I have created a simple example for your application.
1. In hello_world.m, we have
function y = hello_world %#codegen
y = mySubFun;
2. In mySubFun.m, we have
function y = mySubFun %#codegen
y = 'Hello World!';
% Copyright 2010 The MathWorks, Inc.
3. Create a coder project helloworldtest.prj, and add the two MATLAB files into the Entry-Point files, and build the dll.
4. Add the directory for the generated .dll into Windows System Path
5. Open Visual Studio, create an empty win32 Console Application project. Add main.cpp in the source files with the following code
#include "hello_world.h"
#include "mySubFun.h"
#include<stdio.h>
int main(){
char y[12];
mySubFun(y);
for(int i = 0; i < 12; i++)
printf("%c",y[i]);
printf("\n");
return 0;
}
6. In the property of the project. Add the directory of generated dll file to the 'Additional Include Directories' under C/C++ -> General and ‘Additional Library Dependencies' under Linker -> General. Add hello_world.lib in the field 'Additional Dependencies' under Linker -> Input.
Once you complete the above steps, the simple main file we created in step 5 would be able to compile and run correctly by calling the dll generated using MATLAB Coder.
Copyright 2010 The MathWorks, Inc.