MATLAB Answers

0

Using mcc to compile a code that uses a .dll library

Asked by Nuno Almeida on 12 Mar 2012
Latest activity Commented on by kaa417
on 6 Jun 2016
Hi,
I created a program in matlab with a GUI. I want to compile it to have an EXE. In the code, I use an external library, but I only have the files: foo.dll foo.h foo.lib.
In the matlab code I call: loadlibrary ('\foo', '\foo.h');
Later, I call some functions of those libraries, using: calllib('foo', 'Function', param);
I compile it using: mcc -m mycode.m -c
The compiler returns no errors, and a "mycode.exe" file is created. However, when I execute I get:
Error using loadlibrary (line 239) Deployed applications must use a prototype file instead of a header file. To create the prototype, use the loadlibrary mfilename option. Use the prototype file in compiled code. See http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/brb8oui.html for more information.
Error in mycode>mycode_OpeningFcn (line 66)
Error in gui_mainfcn (line 221)
Error in mycode (line 42)
Do you have any tip on how I can overcome this?
Thank you.

  0 Comments

Sign in to comment.

3 Answers

Answer by Kaustubha Govind on 12 Mar 2012
 Accepted Answer

As the error message suggests, you cannot use a header file (.h) with loadlibrary in deployment mode. The link in the error: http://www.mathworks.com/help/toolbox/compiler/brb8oui.html provides step-by-step instructions on how you can generate a MATLAB prototype file (.m) and replace the .h file in your loadlibrary command with the generated .m file. The will fix the error and is also more efficient than providing a .h file. See here for more information about using prototype files with loadlibrary.

  4 Comments

Show 1 older comment
Feng: Sorry about that. Could you see if this page helps: MATLAB Prototype Files.
That page does not give step by step directions. It merely points you to the help for loadlibrary:
How to Create a Prototype File. To create a prototype file, use the mfilename option of the loadlibrary function.
and the loadlibrary page does not give you directions either. All it says is:
loadlibrary(libname,@protofile) uses a prototype file, protofile, in place of a header file.
but that's just how to use an existing prototype file that you have created. It also has this:
'mfilename' Prototype filestring
Prototype file, specified as the comma-separated pair consisting of 'mfilename' and a string. Generates a prototype file in the current folder. The prototype file name must be different from the library name. Use this file in place of a header file when loading the library.
It doesn't tell you how to pass in your .h file. That's not really instructions, much less step by step, on creating a prototype file, is it? In fact the example for building a prototype file step-by-step is missing (all the other uses have examples though, but not for prototype). This page does have some stuff, but it's not really clear what each argument to loadlibrary is.
hi ,have you solve this problem?

Sign in to comment.


Answer by Nuno Almeida on 13 Mar 2012

Thanks. Of course I should have had placed more attention on the error message. Here's how I solved it, and it worked ok. Call this once in the matlab prompt: loadlibrary('foo', 'foo.h', 'mfilename', 'lib_matlab.m');
Then, inside the code, replaced the loadlibrary call for this: loadlibrary ('foo', @lib_matlab);

  0 Comments

Sign in to comment.


Answer by Nuno Almeida on 13 Mar 2012

Furthermore, a new (related) issue:
in my matlab code (which I have just compiled), I call a mex c function ( [y1, y2] = fast_loops(x1, x2); ). This function was written in C, and compiled with mex. Inside the C function, I do some calculations and call a function from matlab ( mexCallMATLAB(2,lhs,1,&rhs, "ADCacquire"); ).
I have this matlab function in the same directory as the rest of the code, libraries, etc. It is a file called "ADCacquire.m" beginning with: [time signal] = ADCacquire(length)
When I run the main code from the MATLAB prompt, it works ok. However, after compiling it with mcc, and running the .EXE, the program runs with no problem until it gets to the part where fast_loops is called. Then an error is returned to the commandline:
" Error using fast_loops
Undefined function 'ADCacquire' for input arguments of type 'double'. "
Do you have any idea on why it is not locating the function, and how can I solve this?
Thank you.

  2 Comments

You need to manually add the file ADCacquire.m to the CTF archive of the compiled application. You can do this with the -a option (if using the mcc command) or drag it under "Other/Additional Files" if using deploytool. Normally, the MATLAB Compiler analyzes your code and automatically locates all the MATLAB files called from your code, but your call to mexCallMATLAB is probably opaque to MATLAB Compiler.
Great!
That was it. mexCallMATLAB was probably hiding it, and adding it manually solved the problem.
Thanks a lot!

Sign in to comment.