幽映每白日,清辉照衣裳。这篇文章主要讲述Android Native:CMake链接错误:GL函数的未定义引用 - 即使包含和链接了EGL和GLESv3相关的知识,希望能为你提供帮助。
我正在尝试将GitHub repo(一个基本的OpenGL渲染立方体)更新为当前的android版本和项目结构。 CMake脚本似乎缺少一些东西,我无法弄清楚。这是一个错误顺序的简单线条问题吗?
系统:Arch Linux- 内核5.0.9,Android Studio 3.5 Canary 13,CMake 3.14.3,Ninja 1.9.0
我在Android Studio中创建了一个新的Native项目,并使用旧数据填充了配置。你可以看到进展here。我一直试图将GL函数库链接到.cpp文件,因为终端输出告诉我存在对某些gl ...函数的未定义引用。
【Android Native(CMake链接错误:GL函数的未定义引用 - 即使包含和链接了EGL和GLESv3)】终端输出:
Build command failed.
Error while executing process /usr/bin/ninja with arguments {-C /home/snobo/projects/AndroidNativeExample/app/.cxx/cmake/debug/armeabi-v7a nativeegl}
ninja: Entering directory `/home/snobo/projects/AndroidNativeExample/app/.cxx/cmake/debug/armeabi-v7a'
[1/2] Building CXX object CMakeFiles/nativeegl.dir/renderer.cpp.o
[2/2] Linking CXX shared library /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so
FAILED: /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so
: &
&
/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi28 --gcc-toolchain=/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mfpu=vfpv3-d16 -fno-addrsig -march=armv7-a -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=c++14 -O0 -fno-limit-debug-info-Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnativeegl.so -o /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so CMakeFiles/nativeegl.dir/jniapi.cpp.o CMakeFiles/nativeegl.dir/renderer.cpp.o/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/28/liblog.so -landroid -lGLESv3 -lEGL -latomic -lm &
&
:
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:230: error: undefined reference to 'glShadeModel'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:236: error: undefined reference to 'glMatrixMode'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:237: error: undefined reference to 'glLoadIdentity'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:238: error: undefined reference to 'glFrustumf'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:262: error: undefined reference to 'glMatrixMode'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:263: error: undefined reference to 'glLoadIdentity'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:264: error: undefined reference to 'glTranslatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:265: error: undefined reference to 'glRotatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:266: error: undefined reference to 'glRotatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:268: error: undefined reference to 'glEnableClientState'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:269: error: undefined reference to 'glEnableClientState'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:272: error: undefined reference to 'glVertexPointer'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:273: error: undefined reference to 'glColorPointer'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
以下是包含上述错误的方法之一:
void Renderer::drawFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -3.0f);
glRotatef(_angle, 0, 1, 0);
glRotatef(_angle*0.25f, 1, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glFrontFace(GL_CW);
glVertexPointer(3, GL_FIXED, 0, vertices);
glColorPointer(4, GL_FIXED, 0, colors);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
_angle += 1.2f;
}
的CMakeLists.txt:
project(AndroidNativeExample C CXX)
cmake_minimum_required(VERSION 3.4.1)add_library(
nativeegl
SHARED
jniapi.cpp renderer.cpp)include_directories(src/main/cpp/include/)find_library( # Sets the name of the path variable.
log-lib
log)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
target_link_libraries(nativeegl Threads::Threads)add_library(app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)target_link_libraries(nativeegl ${app-glue})
target_link_libraries(nativeegl ${log-lib})
target_link_libraries(nativeegl android)
target_link_libraries(nativeegl GLESv3)
target_link_libraries(nativeegl EGL)
据我所知(这确实非常有限),这应该链接所有必要的库,而不是导致错误。但不知何故,一些函数引用未定义。
如果您有任何想法,请告诉我,我会尝试各自并提供反馈。
答案缺少的函数
glShadeModel
等是OpenGL函数,在OpenGL ES中不可用。由于您的项目设法编译(并且在链接时失败),您必须包含来自其他源的OpenGL头,而不是来自Android NDK的OpenGL ES头。
推荐阅读
- 如何使用GPU Video-android获取Watermark并在视频上进行过滤()
- 在不同的Android设备上有哪些有效的PixelFormats()
- 在Android上将int数组转换为Bitmap
- 你可以检测到向上滑动Android头部通知
- androidTv中的NotificationListenerService
- Android,从“最近的应用”中删除应用时取消通知
- Android(以编程方式向通知添加按钮)
- 自定义android通知背景
- 单击firebase的通知时,在设备的浏览器中打开URL而不直接到App