未定义引用`JNI_CreateJavaVM'linux [英] undefined reference to `JNI_CreateJavaVM' linux

查看:824
本文介绍了未定义引用`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();
}


$ b $ p

我搜索了此问题,并尝试了每个解决方案,仍然我得到相同的错误...任何帮助是非常感谢!

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_或者它将无法在运行时找到该lib。

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查找符号的方式最近更改:现在要链接的单元严格按照从左到右的顺序处理,

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.

如果在命令行中没有任何内容需要,则默认忽略库( -lYourLibrary 解决这个问题,在使用它的编译单元之后移动 -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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆