SWIG 接口文件结构导致重复的 Java 函数 [英] SWIG Interface File Structure Causing Duplicate Java Functions
问题描述
我有以下我认为无效的 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 中.有任何想法吗?
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
推荐答案
这里的问题是,当你说 %include
时,就好像你在那个时候直接粘贴了文件的内容(即要求 SWIG 将其全部打包).这意味着 SWIG 已经看到了 func
的两个版本,一个是您明确告诉它的,另一个是您%include
d 中实际存在的标头.
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 %include
d.
有几种方法可以解决这个问题,虽然额外的过载并没有真正造成任何伤害,但它只是嘈杂和混乱.
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.
使用
#ifndef SWIG
在 SWIG 的头文件中隐藏func
的声明.你的头文件会变成:
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
当你 %include
头文件 SWIG 不会看到这个版本的 func
- 这不是问题,因为你明确告诉它另一个版本(这是与 SWIG 的目的兼容)
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)
使用 %ignore
来指示 SWIG 专门忽略此版本的 func
.SWIG 模块文件然后变为:
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"
您还可以更改头文件中 func
的实际声明和定义以及它在代码中实际实现的位置,以使用 unsigned char[20]
而不是 unsigned char*
.
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*
.
这篇关于SWIG 接口文件结构导致重复的 Java 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!