【发布时间】:2016-10-07 11:03:01
【问题描述】:
我想将一个简单的 java 数组传递给 c。
目前我使用以下 .i 文件进行操作。
%module example
%include "arrays_java.i"
%include "example.h"
%{
#include "example.h"
%}
带有arrays_java.i 标头的java 数组被接受。 但它制作了数组的完整副本,这对我来说太慢了。 我尝试使用这些构建类型映射,我可以使用 GetDoubleArray 函数来获取 java 数组的指针。 在我使用 swig 之前,我使用 JNI GetDoubleArrayElements 构建了自己的包装器,这要快得多。
您可以在下面看到我尝试在类型映射中使用 Jni 函数。
%typemap(jtype) (double *values, int lenght) "double[]"
%typemap(jstype) (double *values, int lenght) "double[]"
%typemap(javain) (double *values, int lenght) "$javainput"
%typemap(jni) (double *values, int lenght) "jdoubleArray"
%typemap(in) (double *values, int lenght) {
$1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
$2 = (int) JCALL1(GetArrayLength, jenv, $input);
}
%apply (double * DOUBLE, int LENGTH) { (double * doubleArray, long len) };
但是 swig 只为我构建了一个我应该使用的 javaclass (SWIGTYPE_p_double)。 我只想传递一个简单的java数组并在c中使用这个java数组指针。
希望你能帮助我,谢谢
编辑:
在这里你可以看到我完整的 .i 文件。我纠正了长度失败。同样的问题
%module exampleModule
%include "std_string.i"
%include "std_vector.i"
%include "arrays_java.i"
#include <string>
#include <vector>
%template(nativeDoubleVector) std::vector<double>;
%typemap(jtype) (double *values, int length) "double[]"
%typemap(jstype) (double *values, int length) "double[]"
%typemap(javain) (double *values, int length) "$javainput"
%typemap(jni) (double *values, int length) "jdoubleArray"
%typemap(in) (double *values, int length) {
$1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL);
$2 = (int) JCALL1(GetArrayLength, jenv, $input);
}
%apply (double * values, int length) { (double * doubleArray, long len) };
/* Let's just grab the original header file here */
%include "example.h"
%{
#include "example"
%}
和 .h 文件
class example
{
public:
example(int width, int height);
~example();
test(double *values, int length);
};
我可以看到 -Wall 输出吗?我试了一下,但我在android studio终端中没有输出
【问题讨论】:
-
您对“长度”一词的拼写似乎有误,但还有更多问题。使用 -Wall 参数运行 SWIG 并注意我很确定您会在
%apply上看到的警告。您能否向我们展示 example.h 和您的 .i 文件以及您的类型映射? -
我编辑了我的帖子。谢谢解答
标签: java android c++ arrays swig