Michele Buzzoni
Michele Buzzoni

Reputation: 207

undefined symbol with lib created with jni and OpenCV

I don't understand how can I create a native method which take a Mat and return it modified.

I have this code in java class:

private native void getCanny(long mat);
getCanny(mat.getNativeObjAddr());

and the Mat2Image.h generated:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Mat2Image */

#ifndef _Included_Mat2Image
#define _Included_Mat2Image
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Mat2Image
 * Method:    getCanny
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_Mat2Image_getCanny
  (JNIEnv *, jobject, jlong);

#ifdef __cplusplus
}
#endif
#endif

and this is the .cpp I've made:

#include "Mat2Image.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc.hpp>


JNIEXPORT void JNICALL Java_Mat2Image_getCanny
   (JNIEnv * env, jobject obj, jlong matr){


       cv::Mat* frame=(cv::Mat*)matr;
            cv::cvtColor(*frame, *frame, CV_BGR2GRAY);
            cv::GaussianBlur(*frame, *frame, cv::Size(7,7), 1.5, 1.5);
            cv::Canny(*frame, *frame, 0, 30, 3);


}

but when I try to run the method I have this error:

/usr/lib/jvm/jdk1.8.0_111/bin/java: symbol lookup error: /home/buzzo/Downloads/helloJni-master/jni/libMat.so: undefined symbol: _ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii

Upvotes: 0

Views: 527

Answers (1)

user2543253
user2543253

Reputation: 2173

When you build a .so you usually don't link dependendent libraries with it. You add them when building the executable instead. And the linker doesn't complain about missing symbols when building the .so, but will do so when you build the executable.

For JNI however, this doesn't work becase you use the java executable, which is fixed, and you load your library dynamically. So you have to link the dependendent library into your .so. Find out what library cvtColor and the other functions are in and look up the linker options for your toolchain to see how you need to specify it on the command line. Also be aware that with most linkers the order of arguments matters.

Upvotes: 1

Related Questions