undefined reference to `JNI_CreateJavaVM#39; linux(未定义对 `JNI_CreateJavaVM linux 的引用)
问题描述
我正在尝试熟悉 JNI API,但无法编译示例 C++ 程序.
I'm trying to get familiar with the JNI API but can't get a sample c++ program to compile.
这是我用来编译的命令,下面是我要编译的程序.我得到的错误是:
Here is the command I'm using to compile and below that is the program I'm trying to compile. The error I get is:
/tmp/cczyqqyL.o: In function `main':
/home/nc/Desktop/jni/simple/ctojava/callJava.cpp:16: undefined reference to `JNI_CreateJavaVM'
编译:
g++ -g -I/usr/lib/jvm/java-7-oracle/include/ -I/usr/lib/jvm/java-7-oracle/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/ -ljvm callJava.cpp
C++:
#include <jni.h> /* where everything is defined */
int main(){
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = "-Djava.class.path=/home/nc/Desktop/jni/simple/ctojava/";
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_FALSE;
/* Create the Java VM */
int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args); // this is what it can't find
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("Hello");
jmethodID mid = env->GetStaticMethodID(cls, "staticInt", "(I)I");
env->CallStaticVoidMethod(cls, mid,10);
/* We are done. */
jvm->DestroyJavaVM();
}
我已经搜索了这个问题并尝试了我找到的所有解决方案,但我仍然遇到同样的错误......非常感谢任何帮助!
I've searched for this issue and tried every solution I've found but still I get the same error... Any help is greatly appreciated!
Joni 在下面的回答有效(取决于您的编译器).万一其他人发现:运行编译输出时不要忘记 LD_LIBRARY_PATH=_path_to_your_libjvm.so_ 否则它将无法在运行时找到该库.
Joni's answer below works (depending on your compiler). In case someone else finds this: when running the compiled output don't forget LD_LIBRARY_PATH=_path_to_your_libjvm.so_ or it will not be able to find that lib at runtime.
LD_LIBRARY_PATH=/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server ./a.out
推荐答案
GCC 查找符号的方式最近发生了变化:现在要链接的单元严格从左到右处理,并且对库的引用(-lYourLibrary
) 如果命令行左侧没有需要它们,则会被静默忽略.
The way GCC finds symbols was changed fairly recently: now the units to be linked are processed strictly from left to right, and references to libraries (-lYourLibrary
) are silently ignored if nothing to their left in the command line needs them.
要解决这个问题,请将 -ljvm
移到使用它的编译单元之后,例如移到命令行的最后:
To fix this, move -ljvm
after the compilation units that use it, for example to the very end of the command line:
g++ -g -I/usr/lib/jvm/java-7-oracle/include/ -I/usr/lib/jvm/java-7-oracle/include/linux/
-L/usr/bin/java -L/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/ callJava.cpp -ljvm
这篇关于未定义对 `JNI_CreateJavaVM' linux 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未定义对 `JNI_CreateJavaVM' linux 的引用
基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01