问题
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
回答1:
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.
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 offunc
- that's not a problem though because you explicitly told it about another version (which is compatible for the purposes of SWIG)Use
%ignore
to instruct SWIG to ignore this version offunc
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"
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 useunsigned char[20]
instead ofunsigned char*
.
来源:https://stackoverflow.com/questions/8320201/swig-interface-file-structure-causing-duplicate-java-functions