简体   繁体   English

使用时构建问题 <cmath> 用android ndk

[英]Build issue when using <cmath> with android ndk

I have a simple file stlTest2.cpp like this: 我有一个像这样的简单文件stlTest2.cpp

#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? 为什么在NDK之外使用GCC构建时会有效,而不是使用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. 其直接原因是math.h (通过<cmath>包含)将isnan定义为宏。 Why is the build outside of the ndk not including the #define from math.h , but this is? 为什么ndk之外的构建不包括math.h#define ,但这是? If I comment out the includes in the code, all is fine, but that is not acceptable as this problem repeats itself.... a lot. 如果我在代码中注释掉包含,那么一切都很好,但这是不可接受的,因为这个问题重复了......很多。

The isnan macro was added in C99. 在C99中添加了isnan宏。 In C++11 it was added as a function in the std namespace, overloaded for float and double and long double (so a typical <cmath> header, such as your non-NDK GCC is probably using, might have something like this: 在C ++ 11中,它作为函数添加到std命名空间中,重载为floatdoublelong double (所以典型的<cmath>头文件,例如你的非NDK GCC可能正在使用,可能有这样的东西:

#undef isnan

inline bool isnan(float ...) { ... }

inline bool isnan(double ...) { ... }

inline bool isnan(long double ...) { ... }

), but apparently the NDK hasn't gotten the memo, and is still providing the C99 macro as a convenience. ),但显然NDK还没有得到备忘录,并且仍然提供C99宏作为方便。 ( isnan was never a macro in C++, but before TR1 added std::tr1::isnan , there wasn't really a C++ alternative, so multiple C++ compilers provided the C99 macro.) isnan从来都不是C ++中的宏,但在TR1添加std::tr1::isnan ,没有真正的C ++替代品,因此多个C ++编译器提供了C99宏。)

Do you need to be able to use the compiler-provided isnan or std::isnan ? 你需要能够使用编译器提供的isnanstd::isnan吗? If not, then you can just change this: 如果没有,那么你可以改变这个:

#include <cmath>

to this: 对此:

#include <cmath>

#undef isnan

(and similarly for any other problematic macros, provided you don't need them). (对于任何其他有问题的宏,类似地,只要你不需要它们)。

In $ndk\\sources\\cxx-stl\\gnu-libstdc++\\libs\\armeabi\\include\\bits\\c++config.h (change armeabi to whatever is appropriate) change this: 在$ ndk \\ sources \\ cxx-stl \\ gnu-libstdc ++ \\ libs \\ armeabi \\ include \\ bits \\ c ++ config.h(将armeabi改为任何合适的)改变这个:

/* #undef _GLIBCXX_USE_C99_MATH */

to

#define _GLIBCXX_USE_C99_MATH 1

Then clean and build your project again. 然后再次清理并构建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM