我很难让 SWIG类型映射(javapackage)正常工作。我尝试制作一个简单版本的问题,即使这样似乎也失败了。
富.h:
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
Foo() {};
int doSomething() { return 1 };
};
#endif
酒吧.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"
巴.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"
使用以下命令运行这些:
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:
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);
}
文件已正确生成,但请注意没有 import 语句,因此在任何一个 Bar Java 类中都找不到Foo 。这是一个简单的示例,但仅对 import 语句进行硬编码不是我的选择,因为生成的包含 C JNI 代码的源文件可能具有错误的“Foo”类文件位置。
这似乎是一个非常简单和常见的问题,所以,我想知道我是否遗漏了什么或者我做错了什么。
谢谢您的帮助!