Swig,将c指针转换为float []

时间:2014-09-30 21:14:55

标签: java c arrays pointers swig

我如何将c中的返回值转换为java中的float []

//C Function
inline float32* b2Body::GetWorldPoint(float32 localX, float32 localY)
{
    b2Vec2 val = b2Mul(m_xf, b2Vec2(localX, localY));
    fin[0] = val.x, val.y;
    fin[1] = val.y;
    return fin;
}

如您所见,该函数返回float32 *,它是一个双元素数组。在我的swig文件中,我包含了

%include cpointer.i
%include "arrays_java.i"
%apply float[] {float32*};

但我收到错误:

.cpp: In function '_jfloatArray* Java_com_google_fpl_liquidfun_liquidfunJNI_Body_1getWorldPoint_1_1SWIG_11(JNIEnv*, jclass, jlong, jobject, jfloat, jfloat)':
C:/Users/Garrett/Desktop/liquidfun-master/liquidfun//Box2D/swig/jni/../gen/cpp/armeabi-v7a/liquidfun_wrap.cpp:6147:59: error: 'FillMeInAsSizeCannotBeDeterminedAutomatically' was not declared in this scope
make.exe: *** [obj/local/armeabi-v7a/objs/liquidfun_jni/gen/cpp/armeabi-v7a/liquidfun_wrap.o] Error 1

1 个答案:

答案 0 :(得分:1)

这与an older question on the mailing list基本相同:

%include cpointer.i
%include "arrays_java.i"
%typemap(out) float[] 
%{$result = SWIG_JavaArrayOutFloat(jenv, (float *)$1, 2); %}
%apply float[] {float32*};

有一点需要注意,这将导致SWIG假定所有float*用作返回类型的是一个大小为2的数组。