Matlab 2016 backward compatibility issue with loadlibrary

1 view (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
Kristoffer Walker
Kristoffer Walker on 2 Mar 2017
Hi Phil,
I'm back in business! Thank you!!
Just for the benefit of others, regarding the legality, I compiled in Visual Studio C++ mode using the extern C conditional logic around it as mentioned above. It has always worked in the past in 32-bit Matlab form. But it does not work in 64-bit Matlab. So I expect it was always illegal, but Matlab-32 was more forgiving than Matlab-64. Adding "struct" prefix to struct2* in the public definitions fixes the issue and permits me to once again forward declare struct2.
I also tried the 2nd solution, but if I do this, Visual Studio complains about a redefinition of struct2 in the private *.h file where I'm providing the struct2 member information.
Thanks again, Kris
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)

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!