Header-only C++ library (GLM) doesn't compile with Android-NDK

4.7k views Asked by At

I want to use the GLM (glm.g-truc.net) header-only C++ library in an Android NDK project, but I get compile errors. In the Android.mk, I've added the header search path

LOCAL_CFLAGS += -I/Users/Johannes/Development/glm_include/

and I've also tried to compile using STLport and GNU-STL by setting the following in Application.mk like described in the CPLUSPLUS-SUPPORT document:

APP_STL := stlport_static

or

APP_STL := gnustl_static

respectively. Nothing helps; Those are the errors I get when including <glm/glm.h> and using an glm::ivec2. ndk-build outputs:

Compile++ arm    : wbar <= QCARBase.cpp
In file included from /Users/Johannes/Development/glm_include/glm/glm.hpp:66,
                 from /Users/Johannes/proj/WirtschaftsblattAR/app/android/wbar/jni/QCARBase.cpp:45:
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:240: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:240: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:240: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:251: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:251: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:251: error: expected ')' before 'sizeof'
In file included from /Users/Johannes/Development/glm_include/glm/./core/func_common.hpp:335,
                 from /Users/Johannes/Development/glm_include/glm/glm.hpp:66,
                 from /Users/Johannes/proj/WirtschaftsblattAR/app/android/wbar/jni/QCARBase.cpp:45:
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1202: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1202: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1202: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1217: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1217: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1217: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1228: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1228: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1228: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1240: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1240: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1240: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1253: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1253: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1253: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1268: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1268: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1268: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1279: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1279: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1279: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1291: error: expected unqualified-id before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1291: error: expected ')' before 'sizeof'
/Users/Johannes/Development/glm_include/glm/./core/func_common.inl:1291: error: expected ')' before 'sizeof'
make: *** [/Users/Johannes/proj/WirtschaftsblattAR/app/android/wbar/obj/local/armeabi/objs/wbar/QCARBase.o] Error 1

I'm using the Crystax NDK r6 (www.crystax.net)

4

There are 4 answers

0
j00hi On BEST ANSWER

Somehow I've managed to compile it. Specifying the following options in the Application.mk did the trick:

APP_OPTIM := release
APP_STL := stlport_static
LOCAL_ARM_MODE := thumb
APP_ABI := armeabi armeabi-v7a 

And, I think with the STLport, you can't use RTTI or Exceptions, so don't enable -frtti or -fexceptions in the Android.mk

0
Lukasz On

After a bit of header hopping, I figured out how to fix this with the GNU libstdc++ runtime.

Try defining _GLIBCXX_USE_C99_MATH to 1 before including <glm.hpp>, like this:

#define _GLIBCXX_USE_C99_MATH 1
#include <glm/glm.hpp>

<glm.hpp> includes <cmath>, which in turn includes <math.h>. The _GLIBCXX_USE_C99_MATH macro forces <cmath> to #undef some macros from <math.h> which would otherwise hide some glm functions like isnan(), isinf(), etc.

2
Miguel Grinberg On

Try building a small sample test app like this:

#include <glm/glm.h>
int main(int argc, char* argv[])
{
    return 0;
}

Does that work?

If it does, then I'm going to bet that in your app, you have your glm.h include after some #define that has a symbol collision with a symbol that is used in glm. Your #define is probably making use of the sizeof keyword and that is being substituted in the glm lines that have errors.

A possible solution would be to move the glm.h include above any other #includes and/or #defines, if you make it the first thing in the file you may bypass the problem.

A better solution would be to try to avoid #defines and use inline functions whenever possible.

I hope this helps.

0
Faris M On

I'm running into the same issue when I try to compile glm against gnustl_static, but I don't have any issues when I try to compile with stlport_static. My only suggestion would be to try declaring an stlport dependency using LOCAL_STATIC_LIBRARIES in your shared library module.

Also, have you tried using stlport_shared? stlport has both shared and static libraries, whereas gnustl only has a static version (but supports exceptions/RTTI).