How to use GLM in Android NDK Application

2020-05-28 22:50发布

I am currently trying to port my OpenGL application to Android and am stuck on how to import and build GLM http://glm.g-truc.net/ properly. I have no trouble using GLM in standard C++ apps, however I am pretty new to the NDK. I have tried all other solutions posted around the web with no luck. Here is what I have so far:

I am using the latest version of GLM (0.9.4)

My .cpp file contains:

    #include <glm\glm.hpp>

My Android.mk file looks like:

    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)

**\Development\OpenGL\glm-0.4.0** is the location of the GLM files on my C drive

Upon building, I receive this error:

   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

This resembles codemonkey's problem https://gamedev.stackexchange.com/questions/47128/android-ndk-build-cant-find-glm-headers where the 'APP_STL := gnustl_static' was suggested.

It appears that my source files are correctly setup, however there is some sort of compiler problem that I cannot identify. Any help is greatly appreciated!

2条回答
够拽才男人
2楼-- · 2020-05-28 23:42

Follow this solution if you are using Android Studio.

First, download OpenGL Mathematics library here

Second, extract and copy folder "../glm/glm" to your project location at "../app/src/main/cpp"

Third, on CMakeList.txt, add the following:

# 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)

Fourth, on 'buidl.griddle' (Mobile App), make sure you have right path to your CMakeList

 externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
 }

Fifth, include glm headers to your source file:

// 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>

Sample is available at android-ndk, see Android Endless Tunnel Game

查看更多
祖国的老花朵
3楼-- · 2020-05-28 23:44

Sam Hocevar's answer to codemonkeys problem is correct. it's not glm that's the problem. It's the "limits" header file used by glm that's the problem.

If Sam's answer does not solve your problem, try changing the toolchain to an earlier version by adding the following to your Application.mk file:

NDK_TOOLCHAIN_VERSION=4.4.3

And make sure that the STL includes for your project in Eclipse correspond with the toolchain. Go to project->properties->C/C++ General->Paths and Symbols

make sure the following directories are included:

EDIT : these are only examples; make sure you use the correct platform and 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

EDIT : remark on first directory removed; seems glm looks for the limits file provided by the current stl implementation

查看更多
登录 后发表回答