c++ - 带有 OpenGLES2 的 Android NDK 未定义对 glcalls 的引用

标签 c++ android-ndk opengl-es-2.0

按照教程:http://www.learnopengles.com/calling-opengl-from-android-using-the-ndk/ .
创建了所有必要的文件,但在尝试构建时出现错误:

D:\svn-Genicap3D\trunk\frontend_android\Genicap3D\app\src\main\jni\core\game.cpp
Error:(6) undefined reference to 'glClearColor'
Error:(14) undefined reference to `glClear'

游戏.cpp

#include "game.h"
#include <GLES2/gl2.h>

void on_surface_created() {
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}

void on_surface_changed() {
    // No-op
}

void on_draw_frame() {
    glClear(GL_COLOR_BUFFER_BIT);
}

android.mk

LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)

# Here we give our module name and source file(s)

LOCAL_MODULE    := libgame
LOCAL_CFLAGS    := -Wall -Wextra
LOCAL_LDLIBS :=  -llog -lnativehelper -lGLESv2
LOCAL_CPP_FEATURES += exceptions
LOCAL_SHARED_LIBRARIES := liblog libnativehelper libGLESv2
LOCAL_SRC_FILES := coreBridge.cpp core/game.cpp

# To build the whole .so
FILE_LIST := $(wildcard $(LOCAL_PATH)/../../../core/src/*.cpp)
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%) LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../core/include


include $(BUILD_SHARED_LIBRARY)

game.cpp 和.h 位于核心 map 中,与.mk 文件位于同一 map 中

我试图从其他答案中包含很多内容,但似乎都没有用。 欢迎索取更多文件。

最佳答案

当您使用 Android Studio 时,默认情况下会忽略您的 Makefile,并且会即时生成新的 Makefile,因此未正确引用 OpenGL ES2 库。

此功能是 Android Studio 目前支持 NDK 构建的方式,但它已被弃用,而更好的方法正在开发中。

您仍然可以选择使用它并在您的 build.gradle 中指定您需要链接到 OpenGL ES:

android {
 ...
 defaultConfig {
        ndk {
            moduleName "game"
            ldLibs "GLESv2"               
        }
    }
}

但在我看来,目前最好的办法是停用内置的 NDK 支持并改用您的 Makefile:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

    android {
        ...

        sourceSets.main {
            jniLibs.srcDir 'src/main/libs' //set .so files directory to libs
            jni.srcDirs = [] //disable automatic ndk-build call
        }

        // call regular ndk-build(.cmd) script from app directory
        task ndkBuild(type: Exec) {
            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
            } else {
                commandLine 'ndk-build', '-C', file('src/main').absolutePath
            }
        }

        tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
    }

关于c++ - 带有 OpenGLES2 的 Android NDK 未定义对 glcalls 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632750/

相关文章:

android - 如何包含来自不同项目库的 .so 文件?

未找到 Android NDK 构建路径

android-ndk - ndk 在主项目源代码树外构建库

opengl-es-2.0 - 哪些 OpenGL ES 2.0 纹理格式可进行颜色、深度或模板渲染?

java - 如何使用 OpenGL ES 2.0 和 Java (Android) 使用索引缓冲区绘制顶点

c++ - 使用 CMake 构建 Boost 系统库?

C++ 线程错误 - 类参数的编译时错误

c++ - 如何获取 boost::asio::ip::tcp::socket 的 IP 地址?

c++ - gcc - 使用 #pragma 将 __attribute__((section (".dflash_code"))) 应用于整个源文件

android - GLSurfaceView - 如何制作半透明背景