Matlab 2016 backward compatibility issue with loadlibrary

5 views (last 30 days)
Dear Matlab Aficionados,
I am hoping someone can help me. I am trying to get Matlab 2016a loadlibrary() to accept loading a DLL that has an extern C declared header file with a forward declared struct inside. The same exact code works fin in older versions of Matlab like 2015a, but something changed in the 2016 version. I cannot use an #include statement inside the extern C declaration as a workaround by including the header file with the full definition of the struct in question because that full definition contains C++ classes. Is there anything I can do that will fix this issue? Can the older behavior of permitting forward declarations be reinstated in the future? It sure would save me a lot of code modification if I can find a solution.
This is what I mean:
inside "dll.h":
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int a;
double *data;
} struct1;
struct struct2;
}
inside "dll_private.h"
#include "classes.h"
struct struct2
{
className objectName;
};
UPDATE: I just reinstalled Matlab 2015b to see if I can reproduce the issue. The issue exists there as well. I am thinking this error is compiler-specific. When I reinstalled Matlab, I removed a lot of previous Matlab-related things. I also removed the old Visual Studio 2012 and replaced with Visual Studio 2015. The error I get is this:
Error using loadlibrary
Building DLL_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\Temp\include" /Zp8 /W3 /nologo -I"C:\Temp" -I"C:\Temp\inc"
"DLL_thunk_pcwin64.c" -LD -Fe"DLL_thunk_pcwin64.dll" DLL_thunk_pcwin64.c
C:\Temp\inc\DLL.h(727): error C2143: syntax error: missing '{' before '*'
C:\Temp\inc\DLL.h(733): error C2081: 'ForwardDeclaredStructName': name in formal parameter list illegal
My understanding is to list the compiler that Matlab can see, do "mex -setup":
mex -setup
MEX configured to use 'Microsoft Visual C++ 2015 Professional (C)' for C language compilation.
What can I do to get the original functionality that I used to have of being able to use loadlibrary with a forward declared struct pointer? Do I need to install Visual Studio 2012 and then change the default compiler to use it using "mex -setup"?
Update: I also tried 2015b (64bit) with Visual Studio 2012 DLL compiled in 64-bit (I used mex -setup to change to the VS2012 compiler after installing VS2012). I also recompiled the DLL with VS2012. The issue still persists. I am wondering if this is a 32 vs. 64-bit issue as Phil hinted at below.
Kind regards, Kris Walker

Accepted Answer

Philip Borghesani
Philip Borghesani on 1 Mar 2017
Your header file is c++ code and thunk files are c there is no way this could have worked in 64 bit MATLAB. Was your previous MATLAB 32 bit? If so then you need to rebuild all libraries for 64 bit and fix the header so it will compile as c code. That usually evolves wrapping extern "C" { with an ifdef:
#ifdef __cplusplus
extern "C" {
#endif
... /* extern c code */
#ifdef __cplusplus
}
#endif
  6 Comments
Philip Borghesani
Philip Borghesani on 2 Mar 2017
32 bit MATLAB does not need thunk files and therefor does not actualy compile any code with the header used for loadlibrary. As a result of this it can be much more forgiving about the code in the header file.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!