2017-05-06 20 views
0

JNIを使​​ってjavaでC++コードを実行する方法がわかりません。 私はメイクファイルにいくつかのエラーがあると思う、私はいくつかのライブラリがないと思う。jniアプリケーションのmakefileにlibを追加

私は、このJavaクラスのコードがあります。

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

とMat2Image.hを生成:

/* 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 

、これは私が作ったの.cppです:

#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); 


} 

これは私のメークファイルです:

# Define a variable for classpath 
CLASS_PATH = ../bin 

# Debug: -g3=compile with extra debugg infos. -ggdbg3=include things like macro defenitions. -O0=turn off optimizations. 
DEBUGFLAGS = -g3 -ggdb3 -O0 
CFLAGS = $(DEBUGFLAGS) 

# Define a virtual path for .class in the bin directory 
vpath %.class $(CLASS_PATH) 

all : libMat.so 

# [email protected] matches the target, $< matches the first dependancy 
libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< 

# [email protected] matches the target, $< matches the first dependancy 
libMat.o : Mat2Image.cpp Mat2Image.h 
    g++ $(CFLAGS) -fPIC -I/usr/lib/jvm/jdk1.8.0_111/include -I/usr/lib/jvm/jdk1.8.0_111/include/linux -c $< -o [email protected] 

# $* matches the target filename without the extension 
# manually this would be: javah -classpath ../bin HelloJNI 
HelloJNI.h : Mat2Image.class 
    javah -classpath $(CLASS_PATH) $* 

clean : 
    rm -f Mat2Image.h libMat.o libMat.so 

が、私はこのエラーを持っている方法を実行しよう:

/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 

私はこの問題は、メイクファイル、どのように私はそれを編集することができていると思いますか?

+0

誰も私を助けることができますか?してください... –

答えて

0

あなたのコードはリンクされていないシンボルを使用しているようです。

あなたの共有ライブラリをopencvにリンクすることをお勧めしますか?

共有ライブラリがJNIから使用されているサンプルコードを見たい場合は、ここで見てみましょう:OpenCVのライブラリがLD_LIBRARY_PATH上にないようなあなたの場合には

https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo023

を、それが見えます。

私は

libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< -lopencv_imgproc 

libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< 

を変更するには、この最終的なメイクファイルである:私はメイクファイルを変更することで解決

+0

あなたはJNIを使​​用するための素晴らしいガイドがあります!私はそれを読むでしょう! –

+0

私はmakefileを変更して解決しました –

+0

素晴らしい! JNIとお楽しみください! – mko

0

# Define a variable for classpath 
CLASS_PATH = ../bin 
# Debug: -g3=compile with extra debugg infos. -ggdbg3=include things like macro defenitions. -O0=turn off optimizations. 
DEBUGFLAGS = -g3 -ggdb3 -O0 
CFLAGS = $(DEBUGFLAGS) 

# Define a virtual path for .class in the bin directory 
vpath %.class $(CLASS_PATH) 

all : libMat.so 

# [email protected] matches the target, $< matches the first dependancy 
libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< -lopencv_imgproc 

# [email protected] matches the target, $< matches the first dependancy 
libMat.o : Mat2Image.cpp Mat2Image.h 
    g++ $(CFLAGS) -fPIC -I/usr/lib/jvm/jdk1.8.0_111/include -I/usr/lib/jvm/jdk1.8.0_111/include/linux -c $< -o [email protected] 

# $* matches the target filename without the extension 
# manually this would be: javah -classpath ../bin HelloJNI 
HelloJNI.h : Mat2Image.class 
    javah -classpath $(CLASS_PATH) $* 

clean : 
    rm -f libMat.o libMat.so 
関連する問題