0

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
c12
  • 9,557
  • 48
  • 157
  • 253

1 Answers1

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 %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*.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 1
    I used option #2 as we don't want to change the header files. Thanks for all the options and the clear explanation! – c12 Nov 30 '11 at 17:21