如何包装用 __attribute__((packed,aligned(1))) 定义的 C 结构? [英] How to wrap a C structure which is defined with __attribute__((packed,aligned(1)))?

查看:39
本文介绍了如何包装用 __attribute__((packed,aligned(1))) 定义的 C 结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SWIG 中包装使用 __attribute__((packed,aligned(1))) 定义的 C 结构?

How do I wrap a C structure which is defined with __attribute__((packed,aligned(1))) in SWIG?

推荐答案

你几乎不需要做任何事情就可以完成这项工作,只需将#define属性去掉:

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.

这样做的原因是 SWIG 不会生成任何关心对象布局或大小的代码.它也不会生成对其进行任何假设的代码.相反,所有访问/malloc/new 都通过编译器处理,编译器完全按照正常方式编译生成的代码.

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.

这篇关于如何包装用 __attribute__((packed,aligned(1))) 定义的 C 结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆