Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to execute this c++ code on android

i am trying to call native opengl methods from java. Everything compiles ok but i still get this horrible error in android log cat

ERROR/AndroidRuntime(536): java.lang.UnsatisfiedLinkError:init
                          at com.deo.Glut.Init(Native Method)

according to oracle

UnsatisfiedLinkError is Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native

i can't figure out why the emulator is failing to find my native methods. //the native methods in glut.cpp (jni/glut.cpp)

#include<jni.h>//compiled using cgwin and ndk-build on windows xp
#include<gles/gl.h>
#include<math.h>

static void gluPerspective(GLfloat fovy, GLfloat aspect,
               GLfloat zNear, GLfloat zFar)//android ndk lacks glu tool kit (unbelievable)
{
    #define PI 3.1415926535897932f
    GLfloat xmin, xmax, ymin, ymax;

    ymax = zNear * (GLfloat)tan(fovy * PI / 360);
    ymin = -ymax;
    xmin = ymin * aspect;
    xmax = ymax * aspect;

    glFrustumx((GLfixed)(xmin * 65536), (GLfixed)(xmax * 65536),
               (GLfixed)(ymin * 65536), (GLfixed)(ymax * 65536),
               (GLfixed)(zNear * 65536), (GLfixed)(zFar * 65536));
    #undef PI
}

JNIEXPORT void JNICALL Java_com_deo_Glut_display
  (JNIEnv *, jobject)
{
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
}

JNIEXPORT void JNICALL Java_com_deo_Glut_reshape
  (JNIEnv *, jobject, jint width, jint height)
{
glViewport(0,0,width,height);
if(height==0)height=1;//prevent a divide by zero error in case it ever tries to occur
glMatrixMode(GL_PROJECTION);
gluPerspective(50,width/height,1,1000);
glMatrixMode(GL_MODELVIEW);
}

JNIEXPORT void JNICALL Java_com_deo_Glut_init
  (JNIEnv *, jobject)
{}

I first declared then tried to call the above methods from java which is causing the error.

package com.deo;
public class Glut//java class that declares my native methods(src\com\deo\Glut.java)
{
    static
    {
       System.loadLibrary("glut");
    }
    public native void display();
    public native void reshape(int width, int height);
    public native void init();//this is somehow generating an error :(
}

then tried to call them from my custom renderer

public Glut myglut;
public   void onSurfaceCreated(GL10 gl, EGLConfig config)
{   myglut= new Glut();
    myglut.init();
}

Please help download link to project files

like image 853
Dr Deo Avatar asked Jul 25 '26 22:07

Dr Deo


1 Answers

Because your file is a cpp file, you will need extern "C" { } around your exported functions. Otherwise the compiler will mangle the function names and Java won't find the ones it's looking for.

like image 123
Martin Stone Avatar answered Jul 27 '26 13:07

Martin Stone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!