【问题标题】:failed to execute this c++ code on android未能在 android 上执行此 c++ 代码
【发布时间】:2011-06-09 08:20:02
【问题描述】:

我正在尝试从 java 调用本机 opengl 方法。一切编译正常,但我仍然在 android log cat 中遇到这个可怕的错误

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

根据oracle

UnsatisfiedLinkError 如果 Java 虚拟机找不到 适当的母语定义 声明为原生的方法

我无法弄清楚为什么模拟器无法找到我的本地方法。 //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)
{}

我首先声明然后尝试从导致错误的java调用上述方法。

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 :(
}

然后尝试从我的自定义渲染器中调用它们

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

请帮忙 download link to project files

【问题讨论】:

  • Java_com_deo_Glut_init 实际上是从您的 DLL 中导出的吗? (Dependency Walker 可以帮助找出答案)

标签: c++ opengl-es java-native-interface android-ndk


【解决方案1】:

因为您的文件是 cpp 文件,所以您需要在导出的函数周围使用extern "C" { }。否则编译器将破坏函数名称,Java 将找不到它正在寻找的函数名称。

【讨论】:

  • 或者使用javah生成带有正确声明的头文件并#include它
  • 啊哈!我一直在 cpp 代码中注意到 extern c 的东西,但直到现在我都安全地忽略了它:)。现在我知道为什么它应该在那里了:)
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 2021-02-14
  • 2016-03-16
  • 2021-06-06
相关资源
最近更新 更多