【问题标题】:SWIG Interface File Structure Causing Duplicate Java Functions导致重复 Java 函数的 SWIG 接口文件结构
【发布时间】:2011-11-30 01:50:48
【问题描述】:

我认为以下 SWIG 接口文件结构无效。 int func(usigned 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

【问题讨论】:

    标签: swig


    【解决方案1】:

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

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

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

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

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

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

      %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. 您还可以更改头文件中func 的实际声明和定义以及它在代码中实际实现的位置,以使用unsigned char[20] 而不是unsigned char*

    【讨论】:

    • 我使用了选项 #2,因为我们不想更改头文件。感谢所有选项和明确的解释!
    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多