如何在Android NDK应用程序中使用GLM

kra*_*mes 5 android opengl-es android-ndk glm-math

我目前正在尝试将我的OpenGL应用程序移植到Android,并且我仍然坚持如何正确导入和构建GLM http://glm.g-truc.net/.我在标准C++应用程序中使用GLM没有问题,但我对NDK很新.我已尝试在网上发布的所有其他解决方案,但没有运气.这是我到目前为止:

我使用的是最新版本的GLM(0.9.4)

我的.cpp文件包含:

    #include <glm\glm.hpp>
Run Code Online (Sandbox Code Playgroud)

我的Android.mk文件如下所示:

    LOCAL_PATH:= $(call my-dir)

    include $(CLEAR_VARS)

    LOCAL_MODULE    := libgl2jni
    LOCAL_CFLAGS    := -Werror
    LOCAL_SRC_FILES := gl_code.cpp
    LOCAL_LDLIBS    := -llog -lGLESv2

    APP_STL := gnustl_static
    LOCAL_C_INCLUDES += \Development\OpenGL\glm-0.9.4.0\

    include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)

**\Development\OpenGL\glm-0.4.0**是我的C盘上GLM文件的位置

在构建时,我收到此错误:

   In file included from jni/gl_code.cpp:28:0,
       \Development\OpenGL\glm-0.94.0\glm\glm.hpp:86:18: fatal error: limits: No such file or directory
Run Code Online (Sandbox Code Playgroud)

这类似于codemonkey的问题https://gamedev.stackexchange.com/questions/47128/android-ndk-build-cant-find-glm-headers其中"APP_STL:= gnustl_static"建议.

看来我的源文件设置正确,但是有一些我无法识别的编译器问题.任何帮助是极大的赞赏!

小智 5

如果您使用的是 Android Studio,请遵循此解决方案。

首先在这里下载OpenGL数学库

其次,将文件夹“../glm/glm”提取并复制到您的项目位置“../app/src/main/cpp”

第三,在 CMakeList.txt 上,添加以下内容:

# Import the CMakeLists.txt for the glm library
add_subdirectory(glm) # if your CMakeLists is at '../cpp'
# add_subdirectory(src/main/cpp/glm) # if your CMakeLists is at '../app'

# add lib dependencies
target_link_libraries(
# Specifies the target library.
native-lib
# Links the target library to the log library included in the NDK.
GLESv2
glm)
Run Code Online (Sandbox Code Playgroud)

第四,在'build.gradle'(模块应用程序)上,确保你有正确的 CMakeList 路径

 externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
 }
Run Code Online (Sandbox Code Playgroud)

第五,在源文件中包含 glm 头文件:

// open GL libs
#include <GLES2/gl2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/closest_point.hpp>
Run Code Online (Sandbox Code Playgroud)

示例可在 android-ndk 获得,请参阅Android Endless Tunnel Game


Eri*_*rik 1

Sam Hocevar 对 codemonkeys 问题的回答是正确的。这不是glm的问题。问题在于 glm 使用的“limits”头文件。

如果 Sam 的回答不能解决您的问题,请尝试通过将以下内容添加到您的 Application.mk 文件中,将工具链更改为早期版本:

NDK_TOOLCHAIN_VERSION=4.4.3
Run Code Online (Sandbox Code Playgroud)

并确保 Eclipse 中项目的 STL 包含与工具链相对应。转到项目->属性->C/C++ 常规->路径和符号

确保包含以下目录:

编辑:这些只是示例;确保您使用正确的平台和 abi

/Path/To/NDK/sources/platforms/android-9/arch-arm/usr/include
/Path/To/NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/include
/Path/To/NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi-v7a/include
Run Code Online (Sandbox Code Playgroud)

编辑:删除第一个目录的注释;似乎glm寻找当前stl实现提供的限制文件