赞
踩
今天在写一个OpenGL ES 3.0的demo的时候,编译报错,如下所示:
D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp
:162: error: undefined reference to 'glUnmapBuffer'
完整错误日志,如下所示:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:externalNativeBuildDebug'. > Build command failed. Error while executing process C:\Android\sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\GitLab Source\OpenGLESDemo\app\.cxx\cmake\debug\armeabi-v7a opengles-lesson-lib} ninja: Entering directory `D:\GitLab Source\OpenGLESDemo\app\.cxx\cmake\debug\armeabi-v7a' [1/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/lesson1/Native1Lesson.cpp.o [2/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle.cpp.o [3/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle3.cpp.o [4/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/graphics/GLUtils.cpp.o [5/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle2.cpp.o [6/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangleMapBuffers.cpp.o [7/8] Building CXX object CMakeFiles/opengles-lesson-lib.dir/graphics/Matrix.cpp.o [8/8] Linking CXX shared library "D:\GitLab Source\OpenGLESDemo\app\build\intermediates\cmake\debug\obj\armeabi-v7a\libopengles-lesson-lib.so" FAILED: D:/GitLab Source/OpenGLESDemo/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libopengles-lesson-lib.so cmd.exe /C "cd . && C:\Android\SDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libopengles-lesson-lib.so -o "D:\GitLab Source\OpenGLESDemo\app\build\intermediates\cmake\debug\obj\armeabi-v7a\libopengles-lesson-lib.so" CMakeFiles/opengles-lesson-lib.dir/graphics/GLUtils.cpp.o CMakeFiles/opengles-lesson-lib.dir/graphics/Matrix.cpp.o CMakeFiles/opengles-lesson-lib.dir/lesson1/Native1Lesson.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle2.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle3.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangleMapBuffers.cpp.o -lEGL -lGLESv2 -landroid -llog -latomic -lm && cd ." D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:125: error: undefined reference to 'glMapBufferRange' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:138: error: undefined reference to 'glUnmapBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:150: error: undefined reference to 'glMapBufferRange' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:162: error: undefined reference to 'glUnmapBuffer' clang++: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 11s 12 actionable tasks: 7 executed, 5 up-to-date
我们排查上面的错误日志,发现有一条命令是
CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangleMapBuffers.cpp.o
-lEGL -lGLESv2 -landroid -llog -latomic -lm
咋回事,怎么是-lGLESv2
?
查看下CMakeList.txt代码,原来配置GLES版本配置成了GLESv2
target_link_libraries( # Specifies the target library.
opengles-lesson-lib
EGL
GLESv2
android
# Links the target library to the log library
# included in the NDK.
${log-lib})
我们将GLESv2改成GLESv3 即可。
target_link_libraries( # Specifies the target library.
opengles-lesson-lib
EGL
GLESv3 # OpenGL ES 3.0版本
android
# Links the target library to the log library
# included in the NDK.
${log-lib})
然后重新编译一下。
上面将CMakeList.txt代码,原来配置的GLESv2改成GLESv3,那么error: undefined reference to 'glUnmapBuffer'
的问题不存在了。
但是产生了新的问题,如下所示:
D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp
:188: error: undefined reference to 'glDisableVertexAttribArray'
但是我们点开代码,进入gl3.h 可以看到有这个API的。
完整错误日志如下所示:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:externalNativeBuildDebug'. > Build command failed. Error while executing process C:\Android\sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\GitLab Source\OpenGLESDemo\app\.cxx\cmake\debug\armeabi-v7a opengles-lesson-lib} ninja: Entering directory `D:\GitLab Source\OpenGLESDemo\app\.cxx\cmake\debug\armeabi-v7a' [1/1] Linking CXX shared library "D:\GitLab Source\OpenGLESDemo\app\build\intermediates\cmake\debug\obj\armeabi-v7a\libopengles-lesson-lib.so" FAILED: D:/GitLab Source/OpenGLESDemo/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libopengles-lesson-lib.so cmd.exe /C "cd . && C:\Android\SDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libopengles-lesson-lib.so -o "D:\GitLab Source\OpenGLESDemo\app\build\intermediates\cmake\debug\obj\armeabi-v7a\libopengles-lesson-lib.so" CMakeFiles/opengles-lesson-lib.dir/graphics/GLUtils.cpp.o CMakeFiles/opengles-lesson-lib.dir/graphics/Matrix.cpp.o CMakeFiles/opengles-lesson-lib.dir/lesson1/Native1Lesson.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle2.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangle3.cpp.o CMakeFiles/opengles-lesson-lib.dir/triangle/NativeTriangleMapBuffers.cpp.o -lEGL -lGLESv3 -landroid -llog -latomic -lm && cd ." C:/Android/SDK/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: error: cannot find -lGLESv3 D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:78: error: undefined reference to 'glCreateProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:84: error: undefined reference to 'glAttachShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:87: error: undefined reference to 'glAttachShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:90: error: undefined reference to 'glLinkProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:94: error: undefined reference to 'glGetProgramiv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:99: error: undefined reference to 'glGetProgramiv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:103: error: undefined reference to 'glGetProgramInfoLog' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:109: error: undefined reference to 'glDeleteProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:113: error: undefined reference to 'glDeleteShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:114: error: undefined reference to 'glDeleteShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:31: error: undefined reference to 'glCreateShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:37: error: undefined reference to 'glShaderSource' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:40: error: undefined reference to 'glCompileShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:43: error: undefined reference to 'glGetShaderiv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:48: error: undefined reference to 'glGetShaderiv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:53: error: undefined reference to 'glGetShaderInfoLog' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/graphics/GLUtils.cpp:59: error: undefined reference to 'glDeleteShader' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:5: error: undefined reference to 'glGetString' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:131: error: undefined reference to 'glViewport' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:147: error: undefined reference to 'glClearColor' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:148: error: undefined reference to 'glClear' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:151: error: undefined reference to 'glUseProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:154: error: undefined reference to 'glGetUniformLocation' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:155: error: undefined reference to 'glGetAttribLocation' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:156: error: undefined reference to 'glGetAttribLocation' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:10: error: undefined reference to 'glGetError' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:10: error: undefined reference to 'glGetError' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:183: error: undefined reference to 'glVertexAttribPointer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:191: error: undefined reference to 'glEnableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:193: error: undefined reference to 'glVertexAttribPointer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:201: error: undefined reference to 'glEnableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:209: error: undefined reference to 'glUniformMatrix4fv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/lesson1/Native1Lesson.cpp:211: error: undefined reference to 'glDrawArrays' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:76: error: undefined reference to 'glClearColor' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:8: error: undefined reference to 'glGetString' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:84: error: undefined reference to 'glViewport' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:93: error: undefined reference to 'glClear' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:98: error: undefined reference to 'glUseProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:122: error: undefined reference to 'glVertexAttribPointer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:126: error: undefined reference to 'glEnableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle.cpp:138: error: undefined reference to 'glDrawArrays' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:8: error: undefined reference to 'glGetString' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:86: error: undefined reference to 'glClearColor' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:94: error: undefined reference to 'glViewport' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:103: error: undefined reference to 'glClear' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:108: error: undefined reference to 'glUseProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:133: error: undefined reference to 'glVertexAttribPointer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:137: error: undefined reference to 'glEnableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:146: error: undefined reference to 'glVertexAttrib4fv' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:160: error: undefined reference to 'glDrawArrays' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle2.cpp:163: error: undefined reference to 'glDisableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:8: error: undefined reference to 'glGetString' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:88: error: undefined reference to 'glClearColor' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:96: error: undefined reference to 'glViewport' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:105: error: undefined reference to 'glClear' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:110: error: undefined reference to 'glUseProgram' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:115: error: undefined reference to 'glGenBuffers' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:117: error: undefined reference to 'glBindBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:118: error: undefined reference to 'glBufferData' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:120: error: undefined reference to 'glBindBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:121: error: undefined reference to 'glBufferData' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:123: error: undefined reference to 'glBindBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:124: error: undefined reference to 'glBufferData' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:127: error: undefined reference to 'glBindBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:137: error: undefined reference to 'glDrawElements' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:139: error: undefined reference to 'glDisableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangle3.cpp:140: error: undefined reference to 'glDisableVertexAttribArray' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:120: error: undefined reference to 'glGenBuffers' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:123: error: undefined reference to 'glBufferData' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:125: error: undefined reference to 'glMapBufferRange' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:138: error: undefined reference to 'glUnmapBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:150: error: undefined reference to 'glMapBufferRange' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:162: error: undefined reference to 'glUnmapBuffer' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:186: error: undefined reference to 'glDrawElements' D:/GitLab Source/OpenGLESDemo/app/src/main/cpp/triangle/NativeTriangleMapBuffers.cpp:188: error: undefined reference to 'glDisableVertexAttribArray' clang++: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 7s 15 actionable tasks: 3 executed, 12 up-to-date
查看上面的完整错误日志,使用的是GLESv3版本,但是感觉gl3.h里面的API都找不到。
AndroidManifest.xml中声明了OpenGL ES版本,如下所示,应该是没问题的
<uses-feature android:glEsVersion="0x00030002"
android:required="true" />
前面的那个问题,我们修复了OpenGL ES的库版本为GLESv3,并且错误日志也显示使用了这个版本,所以应该也不是这里的问题
查看官网 https://developer.android.com/guide/topics/graphics/opengl,有如下所示的介绍:
Android 可通过开放图形库 (OpenGL®)(特别是 OpenGL ES API)来支持高性能 2D 和 3D 图形。OpenGL 是一种跨平台的图形 API,用于为 3D 图形处理硬件指定标准的软件接口。OpenGL ES 是 OpenGL 规范的一种形式,适用于嵌入式设备。Android 支持多版 OpenGL ES API:
注意:要使设备支持 OpenGL ES 3.0 API,则需要使用由设备制造商提供的此图形管道的一个实现。搭载 Android 4.3 或更低版本的设备可能不支持 OpenGL ES 3.0 API。如需了解如何检查运行时支持的 OpenGL ES 版本,请参阅检查 OpenGL ES 版本。
查看项目的build.gradle文件,发现minSdkVersion 被设置为 16,不支持OpenGL ES 3.0
我们将minSdkVersion 设置为 18,然后重新编译。
好吧,重新编译正常。 运行效果如下,一个彩色的三角形。
要是有OpenGL ES 3.0版本,得做如下几件事情。
<uses-feature android:glEsVersion="0x00030002"
android:required="true" />
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。