简体   繁体   English

jni未定义符号错误

[英]jni undefined symbol error

I'm trying JNI sample code. 我正在尝试JNI示例代码。
(You can get all below sources through github: https://github.com/pilhoon/jni-test ) (您可以通过github获得以下所有资源: https : //github.com/pilhoon/jni-test

  • Sample.java Sample.java
public class Sample
{
  public native int intMethod(int n);
  public native boolean booleanMethod(boolean bool);
  public native String stringMethod(String text);
  public native int intArrayMethod(int[] intArray);

  public static void main(String[] args)
  {
    System.loadLibrary("sample");
    Sample sample = new Sample();
    int     square = sample.intMethod(5);
    boolean bool   = sample.booleanMethod(true);
    String  text   = sample.stringMethod("JAVA");
    int     sum    = sample.intArrayMethod(new int[]{1,1,2,3,5,8,13} );

    System.out.println("intMethod: " + square);
    System.out.println("booleanMethod: " + bool);
    System.out.println("stringMethod: " + text);
    System.out.println("intArrayMethod: " + sum);
  }
}
  • sample.c sample.c文件
#include "sample.h"
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_Sample_intMethod
  (JNIEnv *env, jobject obj, jint num) {
   return num * num;
}

JNIEXPORT jboolean JNICALL Java_Sample_booleanMethod
  (JNIEnv *env, jobject obj, jboolean boolean) {
  return !boolean;
}

JNIEXPORT jstring JNICALL Java_Sample_stringMethod
  (JNIEnv *env, jobject obj, jstring string) {
    const char *str = (*env)->GetStringUTFChars(env, string, 0);
    char cap[128];
    strcpy(cap, str);
    (*env)->ReleaseStringUTFChars(env, string, str);
    return (*env)->NewStringUTF(env, strupr(cap));
}

JNIEXPORT jint JNICALL Java_Sample_intArrayMethod
  (JNIEnv *env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = (*env)->GetArrayLength(env, array);
    jint *body = (*env)->GetIntArrayElements(env, array, 0);
    for (i=0; iReleaseIntArrayElements(env, array, body, 0);
    return sum;
}

void main(){}

#ifdef __cplusplus
}
#endif
  • sample.h sample.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Sample */

#ifndef _Included_Sample
#define _Included_Sample
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Sample
 * Method:    intMethod
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_Sample_intMethod
  (JNIEnv *, jobject, jint);

/*
 * Class:     Sample
 * Method:    booleanMethod
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_Sample_booleanMethod
  (JNIEnv *, jobject, jboolean);

/*
 * Class:     Sample
 * Method:    stringMethod
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Sample_stringMethod
  (JNIEnv *, jobject, jstring);

/*
 * Class:     Sample
 * Method:    intArrayMethod
 * Signature: ([I)I
 */
JNIEXPORT jint JNICALL Java_Sample_intArrayMethod
  (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif

And I compiled these with gcc on CentOS 6.3 我在CentOS 6.3上用gcc编译了它们

prompt$ gcc -c -o sample.o -fPIC sample.c -I /usr/java/jdk1.7.0_07/include/ -I /usr/java/jdk1.7.0_07/include/linux/
prompt$ gcc -shared -o libsample.so sample.o

But when I run 'java Sample', an error occurs. 但是当我运行“ java Sample”时,会发生错误。

java: symbol lookup error: /home/ph/tmp/jni/libsample.so: undefined symbol: strupr

How can I fix this? 我怎样才能解决这个问题?

在包含stdio.h和/或string.h之后,您是否尝试过编译C文件?

strupr is not standard ANSI C. If you write a native C application that references strupr, you will get a link error similar to what you are seeing strupr不是标准的ANSIC。如果编写引用strupr的本机C应用程序,则会出现类似于您看到的链接错误。

$ gcc  -o sample -fPIC Sample.c -I /xxx/include/ -I  /xxx/include/linux/
Sample.c: In function âJava_Sample_stringMethodâ:
Sample.c:23: warning: passing argument 2 of â(*env)->NewStringUTFâ makes pointer from             integer without a cast
Sample.c: In function âmainâ:
Sample.c:40: warning: passing argument 1 of âprintfâ makes pointer from integer without a cast
/tmp/cc6hPBKz.o: In function `Java_Sample_stringMethod':
Sample.c:(.text+0xaa): undefined reference to `strupr'
/tmp/cc6hPBKz.o: In function `main':
Sample.c:(.text+0x103): undefined reference to `strupr'
collect2: ld returned 1 exit status

Solution is to write your own strupr routine. 解决方案是编写自己的strupr例程。

Java seems to have problems with JNI libraries using other libraries. Java似乎对使用其他库的JNI库有问题。 I have the same problem at the moment, where I want to make a JNI library using glib. 目前,我想使用glib创建JNI库时遇到了同样的问题。 Java doesn't want to know the glib functions, though everything compiled and linked fine. Java不想知道glib函数,尽管一切都经过编译和链接很好。

So if you write JNI libs, you can not use any other libs from that code! 因此,如果您编写JNI库,则不能使用该代码中的任何其他库!

Edit: Link any libs statically into your JNI lib. 编辑:将任何库静态链接到您的JNI库。 That should work! 那应该工作!

我也遇到过同样的情况,但是我从代码中删除了strupr函数,例如strupr(cap)=> cap,现在我可以执行了,因为strupr不是标准的ANSIC,所以不能在其中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM