How do I link a library (.dll and/or .lib) to my mex code?
55 views (last 30 days)
Show older comments
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
Philip Borghesani
on 11 Jul 2017
There should be more error output for the loadlibrary call. The line you gave is the compiler command without the actual error output.
Answers (2)
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
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.
See Also
Categories
Find more on C Shared Library Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!