SWIG将生成的类从不同的模块和包导入当前类 [英] SWIG Importing generated class from a different module and package into the current class

查看:120
本文介绍了SWIG将生成的类从不同的模块和包导入当前类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用SWIG typemap(javapackage)正常工作。我尝试做一个简单的版本的问题,甚至这似乎失败。

I'm having difficulty getting the SWIG typemap(javapackage) to work properly. I tried making a simple version of the problem, and even that seems to fail.

foo.h:

#ifndef FOO_H
#define FOO_H

class Foo
{
public:
    Foo() {};
    int doSomething() { return 1 };
};

#endif

bar.h:

#ifndef BAR_H
#define BAR_H

#include "foo.h"

class Bar
{
public:
    Bar() {};
    int doSomething(Foo foo) { return foo.doSomething(); };
};

#endif

Foo.i

%module FooMod

%include "typemaps.i"
%include "stdint.i"

%{
#include "../header/foo.h"
%}

%include "../header/foo.h"

Bar.i

%module BarMod

%import "Foo.i"

%typemap("javapackage") Foo, Foo *, Foo & "com.me.t.foo";

%include "typemaps.i"
%include "stdint.i"

%{
#include "../header/bar.h"
%}

%include "../header/bar.h"

使用以下命令运行这些命令:

Running these with this the following commands:

swig -c++ -java -package com.me.t.foo -outdir ../../src/com/me/t/foo -o ../src/Foo.cpp Foo.i
swig -c++ -java -package com.me.t.bar -outdir ../../src/com/me/t/bar -o ../src/Bar.cpp Bar.i

我得到这个输出:

package com.me.t.bar;

public class Bar {
  private long swigCPtr;
  protected boolean swigCMemOwn;

  protected Bar(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Bar obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        BarModJNI.delete_Bar(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  public Bar() {
    this(BarModJNI.new_Bar(), true);
  }

  public int doSomething(Foo foo) {
    return BarModJNI.Bar_doSomething(swigCPtr, this, Foo.getCPtr(foo), foo);
  }

}

BarModJNI.java:

BarModJNI.java:

package com.me.t.bar;

public class BarModJNI {
  public final static native long new_Bar();
  public final static native int Bar_doSomething(long jarg1, Bar jarg1_, long jarg2, Foo jarg2_);
  public final static native long Bar_getFoo(long jarg1, Bar jarg1_);
  public final static native void delete_Bar(long jarg1);
}

文件生成正确,但注意有语句,因此无法从任何Bar Java类中找到 Foo 。这是一个简单的例子,但只是硬编码一个导入语句对我来说不是一个选项,因为生成的源文件包含C JNI代码可能有错误的Foo类文件的位置。

The files are generated properly, but notice that there is no import statement, so Foo can't be found from either of the Bar Java classes. This is a simple example, but just hard-coding an import statement isn't an option for me since the generated source files containing the C JNI code might have the wrong locations of the "Foo" class files.

这似乎是一个非常简单和常见的问题,所以,我想知道的是如果我错过了一些东西,或者我做错了。

This seems like a very simple and common problem, so, what I'm wondering is if I'm missing something or if I'm doing something wrong.

感谢您的帮助!

推荐答案

遇到相同的问题和找到的答案, 。

Got the same problem and found answer, so posting it for community.

您需要进行3项更改。


  1. 生成代理类(Bar.java)的语句:

  1. Add import statements to generated proxy class (Bar.java):

// Bar.i
%pragma(java) jniclassimports=%{
import com.me.t.foo.Foo;
%}


  • 将import语句添加到生成的JNI包装类(BarModJNI.java ):

  • Add import statements to generated JNI wrapper class (BarModJNI.java):

    // Bar.i
    %typemap(javaimports) Bar %{
    import com.me.t.foo.Foo;
    %}
    


  • 告诉SWIG让 getCPtr 一个公共成员变量,因为Bar类将要访问它:

  • Tell SWIG to make Foo.getCPtr a public member variable because Bar class will want to access it:

    // Foo.i
    SWIG_JAVABODY_PROXY(public, public, SWIGTYPE)
    SWIG_JAVABODY_TYPEWRAPPER(public, public, public, SWIGTYPE)
    


  • 参考:

    • http://www.swig.org/Doc2.0/SWIGDocumentation.html#Java_code_typemaps
    • http://www.swig.org/Doc2.0/SWIGDocumentation.html#Java_imclass_pragmas

    这篇关于SWIG将生成的类从不同的模块和包导入当前类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆