使用 SWIG %apply 重命名 C 结构体属性

发布于 2024-12-21 04:14:25 字数 901 浏览 0 评论 0原文

我的 SWIG 接口文件和我的 Sample.h 头文件中有以下结构。我假设此结构中的 sockaddr、ios_boolean 和 unsigned char 定义是我得到以下生成的类的原因。如果我知道 ios_boolean 和 unsigned char 映射到 Java 端的类型,有没有办法使用 %apply 来摆脱生成的指针类?我尝试过 %apply int {ios_boolean};但后来我得到了 SWIGTYPE_p_boolean.java。有什么想法吗?

%rename (Sample) sample_details_t_;
typedef struct sample_details_t_ {               
    ios_boolean                   is_allowed;           
    unsigned char                 mac[11];        
 } sample_t;


 generates:
 SWIGTYPE_p_unsigned_char.java
 SWIGTYPE_p_ios_boolean.java

例外:

 [exec] ewapi_wrap.c:982: error: `true' undeclared (first use in this function)
 [exec] ewapi_wrap.c:982: error: (Each undeclared identifier is reported only once
 [exec] ewapi_wrap.c:982: error: for each function it appears in.)
 [exec] ewapi_wrap.c:982: error: `false' undeclared (first use in this function

I have the below structure in my SWIG interface file and thusly my sample.h header file. I'm assuming the sockaddr, ios_boolean and unsigned char definitions from this structure are the reason why I get the below generated classes. If I know the type on that ios_boolean and unsigned char map to on the Java side is there a way to use an %apply to get rid of the generated pointer classes? I tried %apply int {ios_boolean}; but then I get a SWIGTYPE_p_boolean.java. Any ideas?

%rename (Sample) sample_details_t_;
typedef struct sample_details_t_ {               
    ios_boolean                   is_allowed;           
    unsigned char                 mac[11];        
 } sample_t;


 generates:
 SWIGTYPE_p_unsigned_char.java
 SWIGTYPE_p_ios_boolean.java

Exception:

 [exec] ewapi_wrap.c:982: error: `true' undeclared (first use in this function)
 [exec] ewapi_wrap.c:982: error: (Each undeclared identifier is reported only once
 [exec] ewapi_wrap.c:982: error: for each function it appears in.)
 [exec] ewapi_wrap.c:982: error: `false' undeclared (first use in this function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-12-28 04:14:25

您可能想做类似的事情:

%include <arrays_java.i>
%rename (Sample) sample_details_t_;
%apply bool { ios_boolean };
typedef struct sample_details_t_ {               
    ios_boolean                   is_allowed;           
    unsigned char                 mac[11];        
 } sample_t;

这将 mac 包装为 short[] (对数组大小进行限制)并将 is_allowed 包装为 Java 端的 boolean 并生成以下文件:

示例.java test.java testJNI.java

确保删除旧版本 SWIG 界面中存在的所有旧 SWIGTYPE_*.java 文件,它们不会自动删除,并且可能无法删除如果您执行类似 javac *.java 的操作,则进行编译。

You probably want to do something like:

%include <arrays_java.i>
%rename (Sample) sample_details_t_;
%apply bool { ios_boolean };
typedef struct sample_details_t_ {               
    ios_boolean                   is_allowed;           
    unsigned char                 mac[11];        
 } sample_t;

This wraps mac as short[] (with constraints on the array size) and is_allowed as boolean on the Java side and results in these files:

Sample.java test.java testJNI.java

Make sure you delete any old SWIGTYPE_*.java files that are lying around from older versions of your SWIG interface, they won't get deleted automatically and might fail to compile if you do something like javac *.java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文