Cannot find function in <GLES2/gl2.h> by Android Native Activity

245 Views Asked by At

I was thinking of studying OpenGL ES with Android Native Activity, but when I use the API of OpenGL ES 2.0 even though I can use OpenGL ES 1.0, I get a message saying "Undefined symbo l !!"

OpenGL ES 2.0 png

1>ANDROID_HOME=D:\aruze\Microsoft\AndroidSDK\25
1>ANT_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Apps\apache-ant-1.9.3
1>JAVA_HOME=C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25
1>NDK_ROOT=D:\aruze\Microsoft\AndroidNDK64\android-ndk-r16b
1>pch.h
1>android_native_app_glue.c
1>startup.cpp
1>System.cpp
1>main.cpp
1>ld: error: undefined symbol: glCreateShader
1>>>> referenced by startup.cpp:47
1>>>>               ARM\Release\startup.o:(loadShader(unsigned int, char const*))
1>>>> referenced by startup.cpp:47
1>>>>               ARM\Release\startup.o:(createProgram(char const*, char const*))
1>>>> referenced by startup.cpp:47
1>>>>               ARM\Release\startup.o:(createProgram(char const*, char const*))
・
・
・
・
1>>>> referenced by startup.cpp:137
1>>>>               ARM\Release\startup.o:(engine_init_display(Engine&))
1>clang: error: linker command failed with exit code 1 (use -v to see invocation)

I tried repairing Visual Studio and Android SDK manager

Is it because I'm debugging on my old smartphone?

=============================== postscript ====================================

property >> Linker >> 入力(input) >> LibraryDependencies

%(LibraryDependencies);GLESv1_CM;GLESv2;EGL;

sry japanese.

1

There are 1 best solutions below

0
On

I don't think your phone is too old for GLES2 since on your screenshot I could see that it uses Android 6 (GLES2 is available since 2.2).

It's difficult to say with certainty without seeing the code (all I could really see from your screenshot was your precompiled header), but considering that you are linking the GLES2 library, included its header and you were able to successfully make it work with GLES 1, the only real way you'd get this linking error is if you created a GLES 1 context instead of a GLES2. What you're probably doing in your "engine_init_display" function is something like this:

const EGLint attributes[] =
{
    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    EGL_BLUE_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_RED_SIZE, 8,
    EGL_NONE
};

EGLint availableConfigurationCount;

eglChooseConfig(display, attributes, nullptr, 0, &availableConfigurationCount);
assert(availableConfigurationCount);

//----- choose configuration and create surface ...

context = eglCreateContext(display, selectedConfiguration, nullptr, nullptr);

which will create an GLES 1 context instead of GLES2, since it did not specify the EGL_RENDERABLE_TYPE attribute and didn't pass any context attributes to eglCreateContext. To create an GLES2 context you need to do something like this:

const EGLint attributes[] =
{
    EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    EGL_BLUE_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_RED_SIZE, 8,
    EGL_NONE
};

EGLint availableConfigurationCount;

eglChooseConfig(display, attributes, nullptr, 0, &availableConfigurationCount);
assert(availableConfigurationCount); // if your phone does not support gles2 this assertion will fail

//----- choose configuration and create surface ...

EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
context = eglCreateContext(display, selectedConfiguration, nullptr, contextAttributes);

Try to see if works, and if it doesn't you'll need to share your code with us so that we can find the problem(s).