我将使用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);
}
}
*:当我没有使用&#39; extern&#39;声明,结果相同。 和头文件..
/* 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)
***[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
我认为这可以通过推出定义&#39;来解决。像这样
#define gx_openPc _Z7gx_openpc
没有别的办法吗?
ADD: nm [共享对象文件]命令的结果:
U _Z7gx_openPc
这意味着没有链接;;;
ADD: Woops,它也是由Makefile ;;;;;(我使用了错误的变量)