JNI/C++编译问题

JNI/C++ compiling problems

本文关键字:问题 编译 C++ JNI      更新时间:2023-10-16

我是C++新手,并尝试编译一个.so文件以通过Java中的JNI运行它。

C++文件夹的目录结构为:

/
/lib/ - this contains a .a file = library.a below
/folder1 - this contains the .cpp .h and .o files

场景 1:

我正在运行以下内容:

g++ -std=c++11 -I$JAVA_HOME/include -Ifolder1/ -I$JAVA_HOME/include/linux -o outputFileName.so inputFileName.cpp -Llib -llibrary.a -fPIC -shared

我收到以下错误:

relocation R_X86_64_32 against '.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC

所有文件和 .so 对象都有 -fPIC 标志。问题出在哪里?

场景 2:

我正在运行以下内容:

g++ -std=c++11 -I$JAVA_HOME/include -Ifolder1/ -I$JAVA_HOME/include/linux -o outputFileName.so inputFileName.cpp -shared

所以没有 -L 和 -l 标志。

我得到 .so 文件并将其放在带有相关命名的/usr/lib 中,即"lib"前缀。当我运行Java程序时,我得到:

symbol lookup error: /usr/lib/outputFileName.so: undefined symbol: _ZN3...

我使用 c++filt 来反编译符号,并得到对类似这样的类的引用:

namespace::Class::Constructor

我检查了类的.cpp文件,构造函数存在。

请帮忙?

请在此处查看示例代码,您可以在其中了解 JNI 如何使用其他共享库。

https://github.com/mkowsiak/jnicookbook/blob/master/recipes/recipeNo023/Makefile

看起来您引用了 .so 文件使用的某种库。

确保将它们放在您的LD_LIBRARY_PATH - 而不仅仅是您的 JNI 库。