Loading C++ shared library DLL into matlab

Hello,
I want to load a C++ DLL library into Matlab (and ultimately use functions from there in Simulink). I know there is a function called loadlibrary(SHRLIB,HDRFILE) but i was told it works only with pure C libraries (when i tried to load my library it kept spewing errors:
...C:\Users\Username\Desktop\SDK_Download\CoolerMasterSDK\SDK\x86\SDKDLL.h:421:1:
warning: null character(s) ignored...
(the same error would fill whole command window and it would start at ...\SDKDLL.h:348:1: and end at \SDKDLL.h:421:1:)
Is there such function but specifically for C++ libraries? Or other means to load external C++ DLL library? Im attaching the library i want to load. I have MATLAB Support for MinGW-w64 C/C++ Compiler version 17.2.0 installed using Add-On Manager.

回答(2 个)

Solely from looking at this:
warning: null character(s) ignored
/ / S D K D l l . h : S D K D l l D L L m a i n h e a d e r f i l e
I would suspect that the file uses UTF-16 encoding but is being read as UTF-8. This would explain the warning about the null characters and the letter spacing in the comment.
It may be that before all else, all the files need to be converted to UTF-8 with an editor that support both unicode encodings.
It may be that UTF-16 is the result of your editing, in which case, make sure your editor saves as UTF-8.

7 个评论

Thank you
I opened SDKDLL.h and the coding was UCS-2 Little Endian, i changed it to UTF-8. The SDKDLL.dll and SDKDLL.lib have ANSI coding, should i change them to UTF-8 too?
With only SDKDLL.h changed to UTF-8 now the output is (Attachment) and the actual error is (Attachment).
ANSI is fine, it's part of the first plane of UTF-8, always encoded as 8-bit. UCS-2 is the first plane of UTF-16, always encoded as 16-bit.
So, it looks like your encoding issues are fixed. Now, you get standard compiler errors.
For some reason, the compiler is failing to compile "winnt.h" (included by "windows.h"). So the problem may be related to your compiler installation rather than the h file now.
I just ran this command to get information about C++/C compiler:
>> myCCompiler = mex.getCompilerConfigurations('C++','Selected')
myCCompiler =
CompilerConfiguration with properties:
Name: 'MinGW64 Compiler (C++)'
Manufacturer: 'GNU'
Language: 'C++'
Version: '5.3.0'
Location: 'C:\ProgramData\MATLAB\SupportPackages\R2017b\3P.instrset\mingw_w64.instrset'
ShortName: 'mingw64-g++'
Priority: 'E'
Details: [1×1 mex.CompilerConfigurationDetails]
LinkerName: 'C:\ProgramData\MATLAB\SupportPackages\R2017b\3P.instrset\mingw_w64.instrset\bin\g++'
LinkerVersion: ''
MexOpt: 'P:\Programy\MATLAB\R2017b\bin\win64\mexopts\mingw64_g++.xml'
>> myCCompiler = mex.getCompilerConfigurations('C','Selected')
myCCompiler =
CompilerConfiguration with properties:
Name: 'MinGW64 Compiler (C)'
Manufacturer: 'GNU'
Language: 'C'
Version: '5.3.0'
Location: 'C:\ProgramData\MATLAB\SupportPackages\R2017b\3P.instrset\mingw_w64.instrset'
ShortName: 'mingw64'
Priority: 'E'
Details: [1×1 mex.CompilerConfigurationDetails]
LinkerName: 'C:\ProgramData\MATLAB\SupportPackages\R2017b\3P.instrset\mingw_w64.instrset\bin\gcc'
LinkerVersion: ''
MexOpt: 'P:\Programy\MATLAB\R2017b\bin\win64\mexopts\mingw64.xml'
Also:
>> mex -setup C
MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. You will be required
to update your code to utilize the new API.
You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
>> mex -setup C++
MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. You will be required
to update your code to utilize the new API.
You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
I installed MinGW64 using Matlab Addon Manager, should i find other compiler? Or I'm checking something wrong? I just uninstalled MinGW, rebooted computer and installed it again and the error output is still same.
Those are warnings, not errors. They have existed as warnings for quite a number of years, from the time MATLAB started creating a 64 bit version.
I installed Visual Studio 2017 Community, new compiler is recognized, here are errors that show up now with with loadlibrary(), full log in attachment.
diary on
loadlibrary('SDKDLL','SDKDLL')
[Warning: Warnings messages were produced while parsing. Check the functions you intend to use for
correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)]
[> In loadlibrary]
Error loading library intermediate output follows.
The actual error is at the end of this output.
(...)
*********
{Error using loadlibrary
Building SDKDLL_thunk_pcwin64 failed. Compiler output is:
cl -I"P:\Programy\MATLAB\R2017b\extern\include" /Zp8 /W3 /nologo
-I"C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64"
-I"C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64" "SDKDLL_thunk_pcwin64.c" -LD
-Fe"SDKDLL_thunk_pcwin64.dll"
SDKDLL_thunk_pcwin64.c
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(25): error C2061: syntax error:
identifier 'KEY_COLOR'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(31): error C2059: syntax error:
'}'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(35): error C2061: syntax error:
identifier 'COLOR_MATRIX'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2061: syntax error:
identifier 'KeyColor'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2059: syntax error:
';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2059: syntax error:
'['
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(38): error C2059: syntax error:
'}'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2146: syntax
error: missing ')' before identifier 'devIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2061: syntax
error: identifier 'devIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2061: syntax
error: identifier 'GetDeviceLayout'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2059: syntax
error: '<parameter-list>'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2146: syntax
error: missing ')' before identifier 'iEffectIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2061: syntax
error: identifier 'iEffectIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2143: syntax
error: missing ')' before '='
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2072:
'RefreshLed': initialization of a function
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2146: syntax
error: missing ')' before identifier 'colorMatrix'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2061: syntax
error: identifier 'colorMatrix'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2059: syntax
error: ')'
SDKDLL_thunk_pcwin64.c(59): error C2065: 'DEVICE_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(59): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(59): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2065: 'DEVICE_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(60): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(60): error C2059: syntax error: ')'
SDKDLL_thunk_pcwin64.c(61): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(62): error C2061: syntax error: identifier 'DEVICE_INDEX'
SDKDLL_thunk_pcwin64.c(72): error C2061: syntax error: identifier 'LAYOUT_KEYBOARDThunk'
SDKDLL_thunk_pcwin64.c(72): error C2059: syntax error: ';'
SDKDLL_thunk_pcwin64.c(72): error C2059: syntax error: '<parameter-list>'
SDKDLL_thunk_pcwin64.c(89): error C2065: 'EFF_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(89): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(89): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2065: 'EFF_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(90): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(90): error C2059: syntax error: ')'
SDKDLL_thunk_pcwin64.c(91): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(92): error C2061: syntax error: identifier 'EFF_INDEX'
SDKDLL_thunk_pcwin64.c(96): warning C4013: 'falseThunk' undefined; assuming extern returning int
SDKDLL_thunk_pcwin64.c(96): error C2143: syntax error: missing ')' before 'type'
SDKDLL_thunk_pcwin64.c(96): error C2099: initializer is not a constant
SDKDLL_thunk_pcwin64.c(122): error C2065: 'COLOR_MATRIX': undeclared identifier
SDKDLL_thunk_pcwin64.c(122): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(122): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2065: 'COLOR_MATRIX': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(123): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(123): error C2059: syntax error: ')'
SDKDLL_
@Multiplexer,
I've just installed the support package for mingw and get the same errors as you get. I don't really have time to investigate why it's not compiling windows.h properly but in any case, it's only there to define a few types, so you could just omit the #include <windows.h> and define these types explicitly:
//replacements for types used by the DLL normally defined in windows.h
typedef char TCHAR; %because _UNICODE is not defined
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
#define CALLBACK __stdcall
But you also have the issue that your header file is definitively C++ and not valid C. The structures have constructors (not allowed in C), the structures are type definitions (so struct XX {...}; needs to be replaced by typedef struct XX {...} XX; to be valid C syntax), the enums are also type definitions (so enum XX {...}; needs to be replaced by typedef enum XX {...} XX;, one function has an optional input with default value (not allowed in C)
Even after fixing all of these, and not getting any compilation error or warning, loadlibray then errors with 'SDK.dll' is not a valid shared library. It may be because of an incorrect modification I've done or it may be that indeed that dll can't be loaded by matlab.
Attached, my modified SDKDLL.h (renamed to .txt so it can be uploaded). Unfortunately, I won't have time to investigate this further today.
edit: formatting gone awry
Guillaume, I wish there was a way to plus 1 a comment. I think you correctly covered all the major issues.
I expect the reason the dll does not load for you is that you don't have the needed hardware and driver installed to use the Cooler master SDK.

请先登录,再进行评论。

Loadlibrary works for C or C++ libraries that have C interfaces. It appears to me (from a quick look at the header file) that the CoolerMasterSDK has a C interface that is partly broken and was never actually tested from C.
It is possible that the header can be fixed to be compatible with Loadlibrary but it may take a bit of work. Many of the problems I see have been addressed in one or more questions here. If you wish more help I suggest posting the line you used to load the library and the first error message you see here.
To get you started
  1. If you are using 64 bit MATLAB you want the files from x64 not x86.
  2. The line near line 64: extern "C" { needs a protective guard it is c++ code
  3. This file needs windows.h add
#include <windows.h>
to the top of it

1 个评论

Thank you for reply, by protective guard you meant "C" guard or include guard or something else?
The command window output was too large but i figured out how to save it to diary file, I attached the output to this comment, with the original SDKDLL.h file, the first error to appear was:
loadlibrary('SDKDLL','SDKDLL')
{Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Users\Username\Desktop\SDK_Download\CoolerMaster
SDK\SDK\x64\SDKDLL.h:1:4: warning: null character(s) ignored
 / / S D K D l l . h : S D K D l l D L L m a i n h e a d e r f i l e
^
I started reading about the extern "C" and based on this topic https://stackoverflow.com/questions/2168241/is-it-required-to-add-extern-c-in-source-file-also i added protective guard around extern "C" in line 61, 63, 218, 220, 5, 6, 222 and added #include windows.h
Called it again with loadlibrary and here is first error that appeared, full log is in attachments as well as modified library.
loadlibrary('SDKDLL','SDKDLL')
{Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Users\Username\Desktop\SDK_Download\CoolerMaster
SDK\SDK\x64\SDKDLL.h:1:4: warning: null character(s) ignored
 / / S D K D l l . h : S D K D l l D L L m a i n h e a d e r f i l e
^
I also tried moving #include windows.h inside the extern "C" {...} but error remained.
I started reading about extern "C" itself too, looking at this thread https://stackoverflow.com/questions/2168241/is-it-required-to-add-extern-c-in-source-file-also
>> loadlibrary('SDKDLL','SDKDLL')

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 C++ Shared Library Integration 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by