将库添加到jni应用程序的makefile 我不理解如何使用JNI在java中运行C++代码。 我认为makefile中有一些错误,我认为缺少一些lib

将库添加到jni应用程序的makefile 我不理解如何使用JNI在java中运行C++代码。 我认为makefile中有一些错误,我认为缺少一些lib,java,c++,makefile,java-native-interface,lib,Java,C++,Makefile,Java Native Interface,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 #defi

我在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

我认为问题在于makefile,如何编辑它?

您的代码似乎使用了未链接的符号

我建议将您的共享库链接到opencv

如果您想查看从JNI使用共享库的示例代码,请查看以下内容:


在您的情况下,opencv库似乎不在LD_library_路径上。

我通过修改makefile解决了这个问题:

# 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

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

# $@ 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 $@

# $* 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
# 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

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

# $@ 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 $@ 

# $* 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
我改变

libMat.so : libMat.o
    g++ $(CFLAGS) -W -shared -o $@ $<
libMat.so:libMat.o
g++$(CFLAGS)-W-shared-o$@$<

libMat.so:libMat.o
g++$(CFLAGS)-W-共享-o$@$<-lopencv\u imgproc
这是最终的生成文件:

# 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

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

# $@ 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 $@

# $* 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
# 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

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

# $@ 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 $@ 

# $* 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
#为类路径定义一个变量
类路径=../bin
#调试:-g3=使用额外的调试信息进行编译-ggdbg3=包括诸如宏防御之类的内容-O0=关闭优化。
DEBUGFLAGS=-g3-ggdb3-O0
CFLAGS=$(调试标志)
#在bin目录中为.class定义虚拟路径
vpath%.class$(类路径)
所有人:libMat.so
#$@匹配目标,$<匹配第一个依赖项
libMat.so:libMat.o
g++$(CFLAGS)-W-共享-o$@$<-lopencv\u imgproc
#$@匹配目标,$<匹配第一个依赖项
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$@
#$*匹配不带扩展名的目标文件名
#手动设置为:javah-classpath../bin HelloJNI
HelloJNI.h:Mat2Image.class
javah-classpath$(类路径)$*
清洁:
rm-f libMat.o libMat.so

没有人能帮我吗?求你了…太好了!享受JNI带来的乐趣!
# 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

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

# $@ 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 $@ 

# $* 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