简体   繁体   English

在Android上完全支持C ++ 11

[英]Complete C++11 support on Android

I'm currently trying to cross-compile a cross-platform library I have previously developed in order to use it on Android. 我目前正在尝试交叉编译我先前开发的跨平台库,以便在Android上使用它。 To do so, I use the arm-linux-androideabi-g++ (4.9) compiler provided by the NDK, and I link the gnu-libstdc++ also present in the NDK. 为此,我使用了NDK提供的arm-linux-androideabi-g ++(4.9)编译器,并链接了NDK中也存在的gnu-libstdc ++。

Unfortunately, the compilation won't succeed due to the use of some C++11 features. 不幸的是,由于使用了某些C ++ 11功能,编译不会成功。 Such features are specific methods present in "string.h" like std::to_string or std::stof, which could be replaced easily by other ones if I have to. 这些功能是“ string.h”中存在的特定方法,例如std :: to_string或std :: stof,如果需要的话,可以很容易地用其他方法替换。 But I also use more complex ones, like things from "future.h" such as std::future and std::async. 但是我也使用更复杂的代码,例如来自“ future.h”的代码,例如std :: future和std :: async。

I've located the reason of the compilation error for "string.h", in the file "ndk/sources/cxx-stl/gnu-libstdc++/4.9/bits/basic_string.h", the following statement returning false (_GLIBCXX_USE_C99 isn't defined): 我在文件“ ndk / sources / cxx-stl / gnu-libstdc ++ / 4.9 / bits / basic_string.h”中找到了“ string.h”编译错误的原因,以下语句返回false(_GLIBCXX_USE_C99尚未定义):

 //basic_string.h 

 #if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
     && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))

 //methods I want to use

 #endif

From what I understood, these restrictions are induced by the Android Bionic libc. 据我了解,这些限制是由Android Bionic libc引起的。

What options do I have to solve this ? 我必须采取什么解决方案?

I already tried to use the CrystaX NDK, but it only solves my "string.h" problem, and I would rather find a more standard solution. 我已经尝试过使用CrystaX NDK,但是它只能解决我的“ string.h”问题,我希望找到一个更标准的解决方案。

How about using an ARM cross-compiler which isn't specific to Android ? 如何使用非Android专用的ARM交叉编译器?

Thanks. 谢谢。

I've finally found a solution to this issue. 我终于找到了解决这个问题的方法。

First, the use of "future.h" methods is fully supported by the gnu-libstdc++ provided by the android NDK, I just missed some inclusions allowing its usage, my bad. 首先,android NDK提供的gnu-libstdc ++完全支持“ future.h”方法的使用,我只是错过了一些允许使用的包容性,这很不好。

Next, I've taken a deep look into my library and I've figured out that the only method actually causing compilation errors was std::to_string. 接下来,我对我的库进行了深入研究,发现实际上导致编译错误的唯一方法是std :: to_string。 So I decided to simply override it with the following : 因此,我决定简单地使用以下内容覆盖它:

#if __ANDROID__

namespace std {
  #ifndef to_string
  inline string to_string(int _Val)
  {    // convert int to string
      char _Buf[256];
      sprintf(_Buf, "%d", _Val);
      return (string(_Buf));
  }
  #endif
}

#endif

I guess that if there is some other unsupported C++11 methods, it's possible to override them as well. 我猜想,如果还有其他一些不受支持的C ++ 11方法,也可以覆盖它们。

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

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