How to call loadlibrary in a compiled application by MATLAB Compiler on Windows?

41 views (last 30 days)
I have a shared library which I use in MATLAB through loadlibrary. However when I compile that application with MATLAB Compiler and run it afterwards I get an error stating a module would be missing.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jun 2023
Edited: MathWorks Support Team on 28 Jun 2023
There are two steps necessary in order to successfully deploy an application which uses the loadlibrary command.
1.) Create a prototype m-file
When deploying loadlibrary you have to create a prototype m-file and use that instead of a header file. 
Let us assume you call loadlibrary like this 
loadlibrary(library, header)
You would then need to call loadlibrary once like this in MATLAB in order to create a prototype m-file:
loadlibrary(library, header, 'mfilename', 'PrototypeName');
After the execution you will see that a PrototypeName.m file was created in the current folder. In the case you use a 64bit MATLAB you will also notice that a library_thunk_pcwin64.dll was created in addition. We will also need that file later one (64bit only).
After you created the prototype m–file you need to adjust the call to loadlibrary to be:
loadlibrary(library, @PrototypeName)
2.) Compile the application
When compiling the application you have to decide if you like to compile the library into the application or to ship it externally.
2.1) Compiling the library into the application
When you like to compile the DLL into the application you have to add the library as shared resource or helper file manually. You can do this with the -a option (if using the mcc command) or by dragging it under Other/Additional Files (as a helper file) if using one of the compiler apps. In addition when working with 64bit MATLAB you also have to include the library_thunk_pcwin64.dll manually.
2.2) Providing the library as external file
When you decide not to include the library into the application you can also copy the library in the same folder as the compiled application without the need to adjust your code. When working with 64bit, you have to compile the  library_thunk_pcwin64.dll into the application. Otherwise the library_thunk_pcwin64.dll cannot be found at runtime resulting in a module not found error.The benefit of this approach is that you can replace the library with a newer version without the need to recompile the whole MATLAB application. BUT replacing the library with a different version works only if the function signatures of the function in the library stay the same because the prototype m-file (and the library_thunk_pcwin64.dll on 64bit) is tied to the function signatures of the functions in the library.
  1 Comment
KAE
KAE on 8 Mar 2024
What if the Matlab function calls a .NET dll? How can I compile that Matlab function? This is in R2023b with Matlab Compiler.

Sign in to comment.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!