How do I link a library (.dll and/or .lib) to my mex code?
Ältere Kommentare anzeigen
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 Kommentare
Philip Borghesani
am 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.
Antworten (2)
Guillaume
am 10 Jul. 2017
1 Stimme
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 Kommentare
James Tursa
am 10 Jul. 2017
Bearbeitet: James Tursa
am 10 Jul. 2017
What does the actual header look like directly from the vendor, without any of your additions or embellishments? E.g., for the myfun function.
Mex routines can be C++ and call C++ code, but if the function signatures are not extern "C" and you are not using exactly the same compiler you may run into name mangling/decoration issues that will cause unresolved external link errors.
Guillaume
am 10 Jul. 2017
My note about C++/C is only if you call the code directly from matlab. Your mex file can indeed call C++ libraries. However because C++ does not define the binary layout, calling C++ dll/lib from other C++ code is extremely risky if library and calling code have not been compiled at the same time by the same compiler with the same settings. Best case, it will be obvious and crash.
If the header already came with extern "C" then the interface is C anyway, so there's no issue.
A possible reason why you're getting an error while linking the lib file is that it's been compiled as 32 bit whereas you're compiling 64 bit code. If that is the case, you're in trouble. You won't be able to use the lib (other than with 32 bit mex only usable with 32 bit matlab which is not available in recent versions). Your only workaround would be to write a 32 bit executable that loads the dll and relay all the dll inputs/outputs to the mex through interprocess communication. See accessing 32 bit dlls from 64 bit code
Mandeguz
am 11 Jul. 2017
James Tursa
am 11 Jul. 2017
Maybe try something simple. Copy the lib file (let's call it mylib.lib) to the same folder as your myfun.cpp file. Then do this:
mex myfun.cpp mylib.lib
Do you get unresolved link errors?
Mandeguz
am 12 Jul. 2017
Mandeguz
am 25 Jul. 2017
Philip Borghesani
am 11 Jul. 2017
1 Stimme
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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!