2014-03-29 51 views
0

我打算使用JNI。JNI java.lang.UnsatisfiedLinkError:****。***()Z

但結果是...

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); 
    } 
} 

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

+0

是否有證據表明該庫是從靜態構造函數加載成功? –

+0

當我添加System.out.println進行靜態調試時,可以看到java查找對象文件沒有問題。 – Gippeumi

回答

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 

有沒有另一種方式?

ADD: 納米[共享對象文件]命令的結果:

 U _Z7gx_openPc 

這意味着不連接;;;

地址: Woops,這是由Makefile的太;;;;;(我用錯變量)

+0

StackOverflow是一個問答網站,而不是論壇或留言板。請創建一個新的問題,因爲這與原文無關。這將有助於潛在的回答者和有相同問題的人。 –

相關問題