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?
0 个评论
回答(2 个)
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
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?)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C from MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!