0

I have a 2D Java double array:

 double[][] jdestination_list = new double[][];

How do I convert this to:

vector<vector<double>>  destinationListCpp;

My JNI call is as follows:

extern "C"
JNIEXPORT void JNICALL
Java_JNI_Call(JNIEnv *env, jobject thiz, jobjectArray jdestination_list,)

    // always on the lookout for null pointers. Everything we get from Java
    // can be null.
    jsize OuterDim = jdestination_list ? env->GetArrayLength(jdestination_list) : 0;
    std::vector<std::vector<double> > destinationListCpp(OuterDim);

for(jsize i = 0; i < OuterDim; ++i) {
    jdoubleArray inner = static_cast<jdoubleArray>(env->GetObjectArrayElement(jdestination_list, i));

    // again: null pointer check
    if(inner) {
        // Get the inner array length here. It needn't be the same for all
        // inner arrays.
        jsize InnerDim = env->GetArrayLength(inner);
        destinationListCpp[i].resize(InnerDim);

        jdouble *data = env->GetDoubleArrayElements(inner, 0);
        std::copy(data, data + InnerDim, destinationListCpp[i].begin());
        env->ReleaseDoubleArrayElements(inner, data, 0);
    }
}

I keep getting:

undefined reference to void Clas::Java_JNI_Call

Any suggestions on how this can be done?

rrz0
  • 2,182
  • 5
  • 30
  • 65
  • Did you declare Java_JNI_Call inside your `Class` for some reason? It has to be a top-level function for JNI to work (or you have to go the `registerNatives` route) – Botje Oct 02 '19 at 07:54
  • Also, why are you casting each element to `jintArray` if you have a `double[][]`? – Botje Oct 02 '19 at 07:55
  • Thanks for your comments. TI think that is a mistake. It should be `doubleArray inner = static_cast`. Will update code accordingly. Thanks – rrz0 Oct 02 '19 at 08:11

1 Answers1

0

This code generates a C-type function called Java_JNI_Call:

extern "C"
JNIEXPORT void JNICALL
Java_JNI_Call(JNIEnv *env, jobject thiz, jobjectArray jdestination_list,)

That C-type function (note the extern "C"...) is not a member of any class, it can not be overloaded, and the function name does not undergo name mangling as a C++ function does.

This error message is complaining you haven't provided a definition of the C++ function Clas::Java_JNI_Call:

undefined reference to void Clas::Java_JNI_Call

Because you haven't.

JNI calls are technically C functions, not C++ functions. You can get away with using C++ functions via JNI's registerNatives() function on systems where C and C++ calling conventions are compatible, but you have to use top-level or static class methods as the JNI call has no associated C++ object that could be used as a C++ this.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56