Differences in h-file support between Win/Linux for dll/so

21 次查看(过去 30 天)
I am trying to load a library compiled with GCC in both Windows (via MinGW) and Linux. For windows I've compiled it to a .dll and for linux a .so, no problem there. The problem seems to be that on Windows, Matlab doesn't understand the header file.
The problematic parts are equivalent to these:
typedef struct SubStructA{
int a;
} SubStructA;
typedef struct TestStructA{
struct SubStructA a[2];
} TestStructA;
EXPORTED int testFunctionA(TestStructA a){
return sizeof(a);
}
Where EXPORTED is a macro that adds __declspec(dllexport) when compiling on Windows.
When loading it in Linux on R2009a, it works perfectly. On Windows in R2011b and R2012a I get the following error message:
Warning: The data type 'SubStructA#2' used by structure TestStructA does not exist. The structure may not be usable.
Pretty much I've had problems with arrays of structs or enums. Arrays of ints seems to work OK.
Through "mex -setup" I have in windows tried both lcc and Microsoft SDK 7.1 as compilers. As gcc is used in linux, I though that this might be the thing that made the difference but no luck there. I know there are limitations on header files but I have not seen in the documentation that there should be any differences between windows and linux.
I could have sworn I got it to work previously but now I just cannot make it work...
Can I in any way make this work in Windows?

回答(2 个)

Kaustubha Govind
Kaustubha Govind 2012-10-8
Is there a reason, you use:
typedef struct TestStructA{
struct SubStructA a[2];
} TestStructA;
and not:
typedef struct TestStructA{
SubStructA a[2];
} TestStructA;
I think this Stack Overflow discussion explains why the former is non-standard C, and may have trouble being interpreted by some compilers.
  3 个评论
Kaustubha Govind
Kaustubha Govind 2012-10-8
What about if you compile the DLL with Visual Studio instead of MinGW/GCC? I wonder if Visual Studio has trouble reading the binary? Also, are you able to load the library outside of MATLAB (in a Visual Studio project maybe?)
Tomas Björklund
Tomas Björklund 2012-10-11
First, I removed the struct in the code that you commented on and the problem persists. So its definately not that.
Then I removed the [2] part so it said:
typedef struct TestStructA{
struct SubStructA a;
} TestStructA;
I did this WITHOUT recompiling the library. Now Matlab does not complain while loading the library. So it really does not seem to be the library that is the problem.
If the prototype file is saved when running loadlibrary I can see that it is exactly the same when running on Linux and Mac. I would say that it seems that the component that reads the prototype file is the component that is platform dependant. The following line is the problematic line it seeems:
structs.TestStructA.members=struct('a', 'SubStructA#2');
So in Linux it either understands the #2 part for [2] or at least doesn't warn about it. In Windows it either doesn't understand it or warns about it unnecessarily.

请先登录,再进行评论。


Philip Borghesani
Philip Borghesani 2012-10-11
The differences you are seeing are due to bug fixes and enhancements made between R2009a and R2011b not the platform being used.
The following is untested conjecture:
I believe you are getting lucky in R2009a and only structures that are the same size as a pointer can actually work. The bug fix was to prevent the use of improperly defined structures that can cause data corruption and crashes. This can be tested by creating a larger SubStructA (I suggest a double and an int) and using the structsize method of a TestStructA libstruct object to verify that the structure size is the same as sizeof(TestStructA) in c.

类别

Help CenterFile Exchange 中查找有关 Call C from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by