SWIG 接口文件结构导致重复的 Java 函数

发布于 2024-12-18 19:44:02 字数 882 浏览 1 评论 0原文

我有以下 SWIG 接口文件结构,我认为该结构无效。 int func(ussigned char key[20]) 位于 headerThree.h 中。当我离开 %include "HeaderThree.h" 时,我得到一个重复的 int func(SWIGTYPE_p_unsigned_char key); 。如果我删除 %include "HeaderThree.h",其他函数不会显示在生成的 Example.java 文件中。只有 int func(short[] key) 会显示。我想将 SWIG .i 文件配置为不具有 func(SWIGTYPE_p_unsigned_char key) 函数,但将其余函数包含在 HeaderThree.h 中。有什么想法吗?

%module Example
%{
#include "HeaderOne.h"  //has constants and type definitions
#include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis
#include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis

%}
%include "arrays_java.i"
int func(unsigned char key[20]);
%include "HeaderOne.h" //has constants and type definitions
%include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis
%include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis

I have the below SWIG Interface file structure that I feel is invalid. The int func(usigned char key[20]) resides in headerThree.h. When I leave in the %include "HeaderThree.h" I get a duplicate int func(SWIGTYPE_p_unsigned_char key);. If I remove the %include "HeaderThree.h", the other functions do not show up in the generated Example.java file..only the int func(short[] key) does. I would like to configure the SWIG .i file to not have the
func(SWIGTYPE_p_unsigned_char key) function but to have the rest of the functions included in HeaderThree.h. Any ideas?

%module Example
%{
#include "HeaderOne.h"  //has constants and type definitions
#include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis
#include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis

%}
%include "arrays_java.i"
int func(unsigned char key[20]);
%include "HeaderOne.h" //has constants and type definitions
%include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis
%include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis

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

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

发布评论

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

评论(1

水晶透心 2024-12-25 19:44:02

这里的问题是,当您说 %include 时,就好像您直接在该点粘贴了文件的内容(即要求 SWIG 将其全部包装起来)。这意味着 SWIG 已经看到了 func 的两个版本,即您明确告知的版本和您 %include 标头中实际存在的版本。

有几种方法可以解决这个问题,尽管额外的过载并没有真正造成任何伤害,但它只是吵闹和混乱。

  1. 使用 #ifndef SWIG 在 SWIG 的头文件中隐藏 func 的声明。然后你的头文件将变成:

    <前><代码>#ifndef SWIG
    int func(无符号字符 *key);
    #endif

    当您 %include 时,头文件 SWIG 将看不到此版本的 func - 但这不是问题,因为您明确告诉它另一个版本(即与 SWIG 兼容)

  2. 使用 %ignore 指示 SWIG 专门忽略此版本的 func。 SWIG 模块文件将变为:

    %module 示例
    %{
    #include“HeaderOne.h”  
    #include“HeaderTwo.h” 
    #include“HeaderThree.h”
    %}
    
    %包括“arrays_java.i”
    int func(无符号字符键[20]);
    // 该忽略指令仅适用于此时之后看到的内容
    %忽略函数; 
    %包括“HeaderOne.h” 
    %包括“HeaderTwo.h” 
    %包括“HeaderThree.h”
    
  3. 您还可以更改头文件中 func 的实际声明和定义以及它在代码中实际实现的位置,以使用 unsigned char[20 ] 而不是 unsigned char*

The problem here is that when you say %include it is as though you pasted the contents of the file directly at that point (i.e. asked SWIG to wrap it all). This means SWIG has seen both versions of func, the one you explicitly told it about and the one that actually exists in the header you %included.

There's a couple of ways you can fix this, although having the extra overload kicking around doesn't really do any harm, it's just noisy and messy.

  1. Hide the declaration of func in the header file from SWIG using #ifndef SWIG. Your header file would then become:

    #ifndef SWIG
    int func(unsigned char *key);
    #endif
    

    When you %include that header file SWIG won't see this version of func - that's not a problem though because you explicitly told it about another version (which is compatible for the purposes of SWIG)

  2. Use %ignore to instruct SWIG to ignore this version of func specifically. The SWIG module file then becomes:

    %module Example
    %{
    #include "HeaderOne.h"  
    #include "HeaderTwo.h" 
    #include "HeaderThree.h"
    %}
    
    %include "arrays_java.i"
    int func(unsigned char key[20]);
    // This ignore directive only applies to things seen after this point
    %ignore func; 
    %include "HeaderOne.h" 
    %include "HeaderTwo.h" 
    %include "HeaderThree.h"
    
  3. You could also change the actual declaration and definition of func in the header file and the place it's actually implemented in your code to use unsigned char[20] instead of unsigned char*.

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