Search code examples
androidc++android-ndkcmath

Build issue when using <cmath> with android ndk


I have a simple file stlTest2.cpp like this:

#include <jni.h>

#include <cmath>


bool isnan (void);

There is something more complicated in some code I am porting. My question is this. Why would this work when building using GCC outside of the NDK, but not with using the NDK? There error it gives is this:

jni/stlTest2.cpp:6: error: expected unqualified-id before 'sizeof'
jni/stlTest2.cpp:6: error: expected ')' before 'sizeof'

The immediate reason for this is that math.h (included via <cmath>) defines isnan as a macro. Why is the build outside of the ndk not including the #define from math.h, but this is? If I comment out the includes in the code, all is fine, but that is not acceptable as this problem repeats itself.... a lot.


Solution

  • In $ndk\sources\cxx-stl\gnu-libstdc++\libs\armeabi\include\bits\c++config.h (change armeabi to whatever is appropriate) change this:

    /* #undef _GLIBCXX_USE_C99_MATH */
    

    to

    #define _GLIBCXX_USE_C99_MATH 1
    

    Then clean and build your project again.