NDK with C++

I am stuck with drawing a simple line as shown in the following tutorial:
https://stackoverflow.com/questions/22293611/draw-a-line-using-opengles-in-android-ndk-using-c

The Android emulator is still showing "Hello from C++", instead of the line.
How to show the line?
I for example get the error:
error: undefined reference to 'glDrawArrays'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <jni.h>
#include <string>

#include <GLES/gl.h>
#include <GLES2/gl2.h>
#include <GLES/glext.h>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_riffmakerprov5_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    GLfloat line[] = {
            0,0,0,
            100,100,0
    };
    GLfloat colors[] = {
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f
    };
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);
    glVertexPointer(3, GL_FLOAT, 0, line);
    glColorPointer(4, GL_FLOAT, 0, colors);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_LINES, 0, 2);
    glFlush();

    return env->NewStringUTF(hello.c_str());
}
Last edited on
The Android emulator is still showing "Hello from C++", instead of the line.
That just means the executable file is still whatever you previously had on your device -- it hasn't been overwritten yet, because your current code fails to build.

"undefined reference" is specifically a linker error (you're not linking the library that contains the definition for the function). What libraries are you configured to link against?

From a quick search, it looks like you want to be linking to at least these:
 -llog -landroid -lEGL -lGLESv3
https://stackoverflow.com/questions/25438066/android-studio-ndk-undefined-reference-to-gl-functions
Last edited on
Topic archived. No new replies allowed.