问题
How do I wrap a C structure which is defined with __attribute__((packed,aligned(1)))
in SWIG?
回答1:
You need to do almost nothing to make this work, simply #define
the attribute away:
%module test
#define __attribute__(x)
struct foo {} __attribute__((packed,aligned(1)));
Will parse and work as intended.
The reason this works is that SWIG doesn't generate any code that cares about the layout or size of an object. Nor does it generate code that makes any assumptions about it either. Instead all accesses/malloc/new are handled via the compiler which compiles the generated code exactly as normal.
So as long as you make sure that the definition of your struct, as seen by the compiler that compiles the module is correct you won't have any problems.
来源:https://stackoverflow.com/questions/41994788/how-to-wrap-a-c-structure-which-is-defined-with-attribute-packed-aligned1