gpt4 book ai didi

java - jni undefined symbol 错误

转载 作者:行者123 更新时间:2023-11-30 04:12:01 28 4
gpt4 key购买 nike

我正在尝试 JNI 示例代码。
(您可以通过github获取以下所有资源:https://github.com/pilhoon/jni-test)

  • 示例.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);  }}
  • 样本.c
#include "sample.h"#include <string.h>#ifdef __cplusplusextern "C" {#endifJNIEXPORT 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
  • 样本.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class Sample */#ifndef _Included_Sample#define _Included_Sample#ifdef __cplusplusextern "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

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

但是当我运行“java Sample”时,出现错误。

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

我该如何解决这个问题?

最佳答案

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

关于java - jni undefined symbol 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373632/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com