简体   繁体   English

SWIG:使用导入的外部类型

[英]SWIG: using imported external type

I am trying to wrap some libraries written in C++ to interface with JAVA using SWIG. 我正在尝试包装一些用C ++编写的库,以便使用SWIG与JAVA接口。

I have one C++ struct in one library used in one C++ function of another library as an argument. 我在一个库中有一个C ++结构,该库在另一个库的一个C ++函数中用作参数。

common.h 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. 该库经过包装,在JAVA中产生了一个名为eu.irati.librina.ser_obj_t的类。 That's fine. 没关系。 Then I have 那我有

utilities.h utilities.h

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

which is wraped with SWIG producing 用SWIG生产包装

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

in a JAVA class. 在JAVA类中。 Looking around in I found ( SWIG Importing generated class from a different module and package into the current class ) and I added to .i 环顾四周,我发现了( SWIG将生成的类从其他模块导入并封装到当前类中 ),然后将其添加到.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? import被添加到所有java类中...如何将其仅包含在所需的类中?

  2. How can I tell SWIG to change SWIGTYPE_p_rina__cdap_rib__ser_obj_t for eu.irati.librina.ser_obj_t . 我如何告诉SWIG更改SWIGTYPE_p_rina__cdap_rib__ser_obj_teu.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. 注:由于common.hutilities.h在不同的图书馆,我不能把它们放在一起一样痛饮模块中。

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). 正如@Felxo在评论中指出的那样,唯一的解决方案是告诉swig还了解其他库的接口包装(因此,标头及其包装方式)。

What I did: 我做了什么:

  1. I copied common.i into utilities wrapping folder. 我将common.i复制到实用程序包装文件夹中。
  2. I added into utilities.i 我添加到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) 添加代理类的导入(您应该更改Thais使其从Java路径指向您的代理类)

  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). 从复制的common.i删除不必要的说明,例如%template ,请记住,此时,您只是“导入” common.h,而不是包装它(之前已经包装了它)。 Actually, the common.i should only include: 实际上, common.i应该仅包括:

      %{ #include "common.h" }% 

    but no 但不是

      %include "common.h" 
  4. Tell SWIG where are is the common.h as well as where is the utilities.h using the -I option 告诉SWIG哪里是common.h以及哪里是utilities.h使用-I选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM