1

I am trying to wrap some libraries written in C++ to interface with JAVA using SWIG.

I have one C++ struct in one library used in one C++ function of another library as an argument.

common.h

namespace rina {
    namespace cdap_rib {
        typedef struct{
        int size_;
        void* message_;
        } ser_obj_t;
    }
}

This library is wrapped producing a class called eu.irati.librina.ser_obj_t in JAVA. That's fine. Then I have

utilities.h

class IPCPConfigEncoder {
    public:
        void encode (rina::cdap_rib::ser_obj_t& ser_obj);
}

which is wraped with SWIG producing

  public void encode(SWIGTYPE_p_rina__cdap_rib__ser_obj_t ser_obj) {
    ...
}

in a JAVA class. Looking around in I found (SWIG Importing generated class from a different module and package into the current class) and I added to .i

%typemap(javaimports) SWIGTYPE 
%{
    import eu.irati.librina.ser_obj_t;
%}

which produced

import eu.irati.librina.ser_obj_t;
public void encode(SWIGTYPE_p_rina__cdap_rib__ser_obj_t ser_obj) {
    ...
}

Then, I have a couple of questions

  1. The import is added to all the java classes... how can I enclose it only to the desired class?

  2. How can I tell SWIG to change SWIGTYPE_p_rina__cdap_rib__ser_obj_t for eu.irati.librina.ser_obj_t.

Note: Since common.h and utilities.h are in different libraries I can not put them together in the same swig module.

Community
  • 1
  • 1
Bernat
  • 379
  • 1
  • 2
  • 12
  • Does my example at: http://stackoverflow.com/a/30413268/168175 solve this for you? – Flexo Sep 29 '15 at 16:54
  • well, the problem is that both are different modules placed in different folders. When I import the other .i it does not find the includes because it seems that both includes must be in the same folder. – Bernat Sep 30 '15 at 11:55
  • You can use the -I flag to SWIG to set the include search path. – Flexo Sep 30 '15 at 18:37
  • Yes, this worked (together with some changes) but thanks, I'ma answering my own question thanks to your contribution – Bernat Oct 08 '15 at 08:57
  • You should write up an answer yourself so that future readers can benefit from it too. – Flexo Oct 08 '15 at 17:00

1 Answers1

1

As @Felxo pointed in the coments, the only solution is to tell swig to understand also the other libraries interface wraping (so headers and how are they wrapped).

What I did:

  1. I copied common.i into utilities wrapping folder.
  2. I added into utilities.i

    /* this is the "copied" common.i */
    %import "common.i"
    %pragma(java) jniclassimports=%{
    import eu.irati.librina.ser_obj_t;
    %}
    

    to add the import of the proxy class (you should change thais to point to your proxy class from a java path)

  3. Remove from the copied common.i the unecessary instructions like %template, remember that, at this point, you are only "importing" the common.h, not wrapping it (you already wraped it before). Actually, the common.i should only include:

     %{
        #include "common.h"
     }%
    

    but no

     %include "common.h"
    
  4. Tell SWIG where are is the common.h as well as where is the utilities.husing the -I option
Bernat
  • 379
  • 1
  • 2
  • 12