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

50 visualizzazioni (ultimi 30 giorni)
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 Commenti
Philip Borghesani
Philip Borghesani il 11 Lug 2017
There should be more error output for the loadlibrary call. The line you gave is the compiler command without the actual error output.
Mandeguz
Mandeguz il 12 Lug 2017
Modificato: Mandeguz il 12 Lug 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 )

Accedi per commentare.

Risposte (2)

Guillaume
Guillaume il 10 Lug 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 Commenti
Mandeguz
Mandeguz il 12 Lug 2017
This error occured when running mex myfun.cpp mylib.lib
Error using mex
C:\Users\mandeguz\AppData\Local\Temp\mex_25457491823753_5228\myfun.obj:myfun.cpp:(.text+0x103):
undefined reference to `__imp_myfun'
collect2.exe: error: ld returned 1 exit status
And when I change the header file as Philip Borghesani suggested,
Error using mex
C:\Users\mandeguz\AppData\Local\Temp\mex_28574022828818_5228\myfun.obj:myfun.cpp:(.text+0x103):
undefined reference to `__imp_myfun'
collect2.exe: error: ld returned 1 exit status
And when I change it back,
Error using mex
C:\Users\mandeguz\AppData\Local\Temp\mex_28775562444849_5228\myfun.obj:myfun.cpp:(.text+0x103):
undefined reference to `__imp_myfun'
collect2.exe: error: ld returned 1 exit status
which makes me think that the long number next to Temp\mex_ is memory-based, since that's the only thing that changes when I change the references into pointers. I'm not really sure what this message means, but I read somewhere that _imp__ is a pointer that is defined in a .lib file. I'll look more into this.
Mandeguz
Mandeguz il 25 Lug 2017
The DLLs were in 32-bit, just found out

Accedi per commentare.


Philip Borghesani
Philip Borghesani il 11 Lug 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 Commento
Mandeguz
Mandeguz il 12 Lug 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

Accedi per commentare.

Categorie

Scopri di più su C Shared Library Integration in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by