簡體   English   中英

JNI java.lang.UnsatisfiedLinkError:****。***()Z

[英]JNI java.lang.UnsatisfiedLinkError: ****.***()Z

我將使用JNI。

但是結果是...

Exception in thread "main" java.lang.UnsatisfiedLinkError: Graphic.begin()Z
    at Graphic.begin(Native Method)
    at Graphic.main(Graphic.java:22)

編譯:

 objects: g++ -c -fPIC -I. -I../gxlib/include/gxlib -I/usr/lib/jvm/jdk-7-oracle-armhf/include/ -I/usr/lib/jvm/jdk-7-oracle-armhf/include/linux -o a.o a.cpp
 final file: g++ -shared -o libpbjni.so [Object files]

庫已加載。 但是調用函數時出現問題。

碼:

C ++代碼

#include <iostream>
#include <string>

using std::cerr;
using std::endl;
using std::string;

void logerr(string s){
    cerr << "PB: ERROR: " << s << endl;
}

extern "C"{
#include "Graphic.h"
#include <gx.h>

dc_t *scr, *buf;
font_t *font12, *font24, *font36;

//boolean begin()
JNIEXPORT jboolean JNICALL Java_Graphic_begin (JNIEnv * env, jobject o){
    if(GX_SUCCESS != gx_open("/dev/fb1")){
        logerr("Failed to open framebuffer /dev/fb1");
        return false;
    }
    if(NULL == (scr = gx_get_screen_dc())){
        logerr("Failed to get device context");
        gx_close();
        return false;
    }
    if(NULL == (buf = gx_get_compatible_dc(scr))){
        logerr("Failed to get compatible device context");
        gx_release_dc(scr);
        gx_close();
        return false;
    }
    gx_clear(buf, gx_color(0, 0, 0, 255));
    if(NULL == (font12 = gx_open_font("font12.bdf"))){
        logerr("Failed to oepn font font12.bdf");
        gx_release_dc(buf);
        gx_release_dc(scr);
        gx_close();
        return false;
    }
    if(NULL == (font24 = gx_open_font("font24.bdf"))){
        logerr("Failed to oepn font font24.bdf");
        gx_release_dc(buf);
        gx_release_dc(scr);
        gx_close();
        return false;
    }
    if(NULL == (font36 = gx_open_font("font36.bdf"))){
        logerr("Failed to oepn font font36.bdf");
        gx_release_dc(buf);
        gx_release_dc(scr);
        gx_close();
        return false;
    }
    buf->font = font36;
    gx_text_out(buf, 90, 120, "PiBox");
    gx_bitblt(scr, 0, 0, buf, 0, 0, buf->width-1, buf->height-1);
    return true;
}

//int width()
JNIEXPORT jint JNICALL Java_Graphic_width (JNIEnv * env, jobject o){
    return buf->width;
}

//int height()
JNIEXPORT jint JNICALL Java_Graphic_height (JNIEnv * env, jobject o){
    return buf->height;
}

//void end()
JNIEXPORT void JNICALL Java_Graphic_end (JNIEnv * env, jobject o){
    gx_release_dc(buf);
    gx_release_dc(scr);
    gx_close();
}

//void update()
JNIEXPORT void JNICALL Java_Graphic_update (JNIEnv * env, jobject o){
    gx_bitblt(scr, 0, 0, buf, 0, 0, buf->width-1, buf->height-1);
    //backlight_on();
}

//void clear(int r, int g, int b)
JNIEXPORT void JNICALL Java_Graphic_clear (JNIEnv * env, jobject o, jint r, jint g, jint b){
    gx_clear(buf, gx_color(r, g, b, 255));
}

//void textcol(int r, int g, int b)
JNIEXPORT void JNICALL Java_Graphic_textcol (JNIEnv * env, jobject o, jint r, jint g, jint b){
    buf->font_color = gx_color(r, g, b, 255);
}

//void textcol(int r, int g, int b)
JNIEXPORT void JNICALL Java_Graphic_pencol (JNIEnv * env, jobject o, jint r, jint g, jint b){
    gx_pen_color(buf, gx_color(r, g, b, 255));
}

//void brushcol(int r, int g, int b)
JNIEXPORT void JNICALL Java_Graphic_brushcol (JNIEnv * env, jobject o, jint r, jint g, jint b){
    gx_brush_color(buf, gx_color(r, g, b, 255));
}

//void textsize(int size)
JNIEXPORT void JNICALL Java_Graphic_textsize (JNIEnv * env, jobject o, jint size){
    switch(size){
        case 1:{
            buf->font = font12;
            break;
        }case 2:{
            buf->font = font24;
            break;
        }case 3:{
            buf->font = font36;
            break;
        }
    }
}

//void putText(int x, int y, String s)
JNIEXPORT void JNICALL Java_Graphic_putText (JNIEnv *env, jobject o, jint x, jint y, jstring s){
    const char *str = env->GetStringUTFChars(s, 0);
    gx_text_out(buf, x, y, str);
}

//void rect(int sx, int sy, int ex, int ey)
JNIEXPORT void JNICALL Java_Graphic_rect (JNIEnv * env, jobject o, jint sx, jint sy, jint ex, jint ey){
    gx_rectangle(buf, sx, sy, ex, ey);
}

//void plus(int x, int y, String s)
JNIEXPORT void JNICALL Java_Graphic_plus (JNIEnv * env, jobject o, jint x, jint y, jstring){
    gx_line(buf, x-5, y, x+5, y);
    gx_line(buf, x, y-5, x, y+5);
}

//void fillbmp(String filename)
JNIEXPORT void JNICALL Java_Graphic_fillbmp (JNIEnv * env, jobject o, jstring filename){
    const char *str = env->GetStringUTFChars(filename, 0);
    dc_t *b;
    if(!(b = gx_bmp_open(str))){
        logerr("Failed to open ");
        return false;
    }
    gx_bitblt(buf, 0, 0, (dc_t *)b, 0, 0, b->width-1, b->height-1);
    return true;
}

//void pixel(int x, int y, int r, int g, int b)
JNIEXPORT void JNICALL Java_Graphic_pixel (JNIEnv * env, jobject o, jint x, jint y, jint r, jint g, jint b){
    gx_set_pixel(buf, x, y, gx_color(r, g, b, 255));
}

//void line(int sx, int sy, int ex, int ey)
JNIEXPORT void JNICALL Java_Graphic_line (JNIEnv * env, jobject o, jint sx, jint sy, jint ex, jint ey){
    gx_line(buf, sx, sy, ex, ey);
}

}

*:當我不使用'extern'語句時,結果相同。 和頭文件..

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Graphic */

#ifndef _Included_Graphic
#define _Included_Graphic
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Graphic
 * Method:    begin
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_Graphic_begin
  (JNIEnv *, jobject);

/*
 * Class:     Graphic
 * Method:    width
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_Graphic_width
  (JNIEnv *, jobject);

/*
 * Class:     Graphic
 * Method:    height
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_Graphic_height
  (JNIEnv *, jobject);

/*
 * Class:     Graphic
 * Method:    end
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Graphic_end
  (JNIEnv *, jobject);

/*
 * Class:     Graphic
 * Method:    update
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Graphic_update
  (JNIEnv *, jobject);

/*
 * Class:     Graphic
 * Method:    clear
 * Signature: (III)V
 */
JNIEXPORT void JNICALL Java_Graphic_clear
  (JNIEnv *, jobject, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    textcol
 * Signature: (III)V
 */
JNIEXPORT void JNICALL Java_Graphic_textcol
  (JNIEnv *, jobject, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    pencol
 * Signature: (III)V
 */
JNIEXPORT void JNICALL Java_Graphic_pencol
  (JNIEnv *, jobject, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    brushcol
 * Signature: (III)V
 */
JNIEXPORT void JNICALL Java_Graphic_brushcol
  (JNIEnv *, jobject, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    textsize
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_Graphic_textsize
  (JNIEnv *, jobject, jint);

/*
 * Class:     Graphic
 * Method:    putText
 * Signature: (IILjava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Graphic_putText
  (JNIEnv *, jobject, jint, jint, jstring);

/*
 * Class:     Graphic
 * Method:    rect
 * Signature: (IIII)V
 */
JNIEXPORT void JNICALL Java_Graphic_rect
  (JNIEnv *, jobject, jint, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    plus
 * Signature: (IILjava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Graphic_plus
  (JNIEnv *, jobject, jint, jint, jstring);

/*
 * Class:     Graphic
 * Method:    fillbmp
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Graphic_fillbmp
  (JNIEnv *, jobject, jstring);

/*
 * Class:     Graphic
 * Method:    pixel
 * Signature: (IIIII)V
 */
JNIEXPORT void JNICALL Java_Graphic_pixel
  (JNIEnv *, jobject, jint, jint, jint, jint, jint);

/*
 * Class:     Graphic
 * Method:    line
 * Signature: (IIII)V
 */
JNIEXPORT void JNICALL Java_Graphic_line
  (JNIEnv *, jobject, jint, jint, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

和Java代碼

public class Graphic{
    native boolean begin();
    native int width();
    native int height();
    native void end();
    native void update();
    native void clear(int r, int g, int b);
    native void textcol(int r, int g, int b);
    native void pencol(int r, int g, int b);
    native void brushcol(int r, int g, int b);
    native void textsize(int size);
    native void putText(int x, int y, String s);
    native void rect(int sx, int sy, int ex, int ey);
    native void plus(int x, int y, String s);
    native void fillbmp(String filename);
    native void pixel(int x, int y, int r, int g, int b);
    native void line(int sx, int sy, int ex, int ey);
    static { System.loadLibrary("pbjni"); }

    public static void main(String[] args){
        Graphic g = new Graphic();
        g.begin();
        g.clear(255, 0, 0);
    }
}

我的代碼或編譯中有任何問題?

我自己修好了。 問題出在Makefile中;(我使用了錯誤的變量),但是我還有另一個問題..我無法調用某些函數。 (該函數在另一個目標文件中)

***[GLOBAL] info: Loading external library...
***[GLOBAL] ok: Library loaded.
***[MAIN] info: Creating instance of Graphic
***[MAIN] info: Calling method begin
[Dynamic-linking native method Graphic.begin ... JNI]
java: symbol lookup error: /home/pi/pibox/java/libpbjni.so: undefined symbol: _Z7gx_openPc

我認為可以這樣放置“定義”來解決此問題

#define gx_openPc _Z7gx_openpc

沒有別的辦法了嗎?

添加:nm [Shared Object File]命令的結果:

     U _Z7gx_openPc

這意味着沒有鏈接;

ADD:糟糕,這也是Makefile造成的;;;;;(我使用了錯誤的變量)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM