MATLAB Answers

How do I link a library (.dll and/or .lib) to my mex code?

102 views (last 30 days)
Mandeguz
Mandeguz on 10 Jul 2017
Commented: Mandeguz on 25 Jul 2017
I've been working with a company and given three files: a header file (.h), a static library (.lib), and a dynamic library (.dll). The header file has 22 functions written in C++. The static library unzips into 22 dynamic libraries, none of which can be read with notepad nor unzipped with 7-Zip. The main dynamic library can be unzipped, but these files contain .rsrc and .data files that cannot be read with notepad. The company will not give me the source code.
With that being said, I have to implement the 22 functions with MATLAB. I've written 22 mex files but I do not know how to link anything other than the header file to the mex file. I've tried
mex '-LC:\Program Files\MyPath\MyFolder' -lMyLibMinusLibExtension myfun.cpp
It instead gives me five errors saying " skipping incompatible" and then ending with " cannot find -lMyLibMinusLibExtension"
I've also tried using loadlibrary, but it shows this thunk-related error:
Error using loadlibrary
Building MyLibMinusLibExtension_thunk_pcwin64 failed. Compiler output is:
C:\TDM-GCC-64\bin\gcc -I"C:\Program
Files\MATLAB\R2017a\extern\include" -fexceptions
-fno-omit-frame-pointer -I"C:\Program Files\MyPath\MyFolder"
-I"C:\Program Files\MyPath\MyFolder" "MyLibMinusLibExtension_thunk_pcwin64.c"
-o "MyLibMinusLibExtension_thunk_pcwin64.dll" -shared
If anyone can let me know how I'm supposed to link either the .dll or the .lib (or both, I'm not really sure which one is needed or how I'm supposed to check) to my mex files or straight to my MATLAB code, that would be very helpful.
  2 Comments
Mandeguz
Mandeguz on 12 Jul 2017
This is the error output (for some reason I did not catch that the first time):
In file included from MyLibMinusLibExtension_thunk_pcwin64.c:27:0:
C:\Program Files\MyPath\MyFolder/MyLibMinusLibExtension.h:60:9: error:
expected ';', ',' or ')' before '&' token
int & errorCode /** Pump error field */,
^
C:\Program Files\MyPath\MyFolder/MyLibMinusLibExtension.h:31:57: error:
expected ';', ',' or ')' before '&' token
int &
out_one,
^
C:\Program Files\MyPath\MyFolder/MyLibMinusLibExtension.h:32:80: error:
expected ';', ',' or ')' before '&' token
float &
output);
^
In file included from MyLibMinusLibExtension_thunk_pcwin64.c:27:0:
C:\Program Files\MyPath\MyFolder/MyLibMinusLibExtension.h:37:62: error:
expected ';', ',' or ')' before '&' token
unsigned
int
&
value
);
^
All of these errors stem from failing to read C++, even with the "extern C". Examples of what happens when I turn the ampersands into asterisks is below ( or here )

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 10 Jul 2017
The static library unzips into 22 dynamic libraries
The main dynamic library can be unzipped
Huh? Neither static library (.lib) nor dynamic library (.dll) are zip files. They just contain code and you shouldn't be able to unzip them into anything. Both should just contain the (mostly compiled for lib/ fully compiled for dll) code for the 22 functions defined by the header file.
It does not sound like the files you've been given are genuine dll or lib files. I can't even understand why unzipping something positing as a static library would result in dll files.
Note that if the code is genuinely C++, you will not be able to use it from matlab which can only speak C. If any of the 22 exported function use any class or non-pod type in their signature or throw exceptions, then you're in trouble. All exported functions must be surrounded by an extern "C"{...} in the header file.
  7 Comments

Sign in to comment.


Philip Borghesani
Philip Borghesani on 11 Jul 2017
The reference parameters will not work with loadlibrary because c does not support the "&" reference c++ syntax. To load this library using loadlibrary make a copy of the header file and change all & symbols in function declarations to * pointer symbols. The resulting header should be usable by MATLAB to load the library.
  1 Comment
Mandeguz
Mandeguz on 12 Jul 2017
Great point, I think that has the potential to be a huge issue. Unfortunately, I think that since the source code was compiled with C++, I might be in a tough situation because I can't actually change that function, only its inputs.
This error occured when running loadlibrary('mylib')
Error using loadlibrary
There was an error loading the library "C:\Program Files\MyPath\MyFolder\MY_DLL.dll"
'C:\Program Files\MyPath\MyFolder\MY_DLL.dll' has different architecture than the host.
Caused by:
Error using loaddefinedlibrary
'C:\Program Files\MyPath\MyFolder\MY_DLL.dll' has different architecture than the host
Side note: my dll, lib, and header all have the same name

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!