How to Use Std::Stoul and Std::Stoull in Android

How to use std::stoul and std::stoull in Android?

The reason why you cannot use the functions is quite deep rooted, and unfortunately currently unsolvable.

Looking into the libs/armeabi-v7a/include/bits/c++config.h file in the gnu stdlibc++ folder, you'll see this:

...
/* Define if C99 functions or macros from <wchar.h>, <math.h>, <complex.h>,
<stdio.h>, and <stdlib.h> can be used or exposed. */
/* #undef _GLIBCXX_USE_C99 */
...

The above, in conjunction with the following snippet from bits/basic_string.h spells bad news:

...
#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))

/* The definitions of Numeric Conversions [string.conversions] */
#endif
...

Thus, these functions are unusable in the NDK.

Root Cause: The root cause seems to be that the C99 functionality usage has been disabled in the GNU stdlibc++ on the armeabi-v7a platform due to the fact the the Bionic libc does not support complex math (the standard C library on Android is Bionic).

Possible Fix (untested): Explore CrystaX's Android NDK which seems to have extensions over the Vanilla Android NDK.

Note: __GXX_EXPERIMENTAL_CXX0X__ is defined by adding -std=gnu++11 to APP_CXXFLAGS or LOCAL_CXXFLAGS

Detailed Test log: Built using NDK version r8e

jni/Application.mk:

APP_STL := gnustl_static
APP_CXXFLAGS += -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.7

jni/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cxx11
LOCAL_SRC_FILES := cxx11.cpp
include $(BUILD_EXECUTABLE)

jni/cxx11.cpp:

#include <iostream>
#include <string>

int main(int argc, char* argv[]) {
#if defined(__GXX_EXPERIMENTAL_CXX0X__)
std::cout<<"__GXX_EXPERIMENTAL_CXX0X__ defined."<<std::endl;
#else
std::cout<<"__GXX_EXPERIMENTAL_CXX0X__ not defined."<<std::endl;
#endif

#if defined(_GLIBCXX_USE_C99)
std::cout<<"_GLIBCXX_USE_C99 defined."<<std::endl;
#else
std::cout<<"_GLIBCXX_USE_C99 not defined."<<std::endl;
#endif

#if defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
std::cout<<"_GLIBCXX_HAVE_BROKEN_VSWPRINTF defined."<<std::endl;
#else
std::cout<<"_GLIBCXX_HAVE_BROKEN_VSWPRINTF not defined."<<std::endl;
#endif

#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
std::string s="1";
std::cout<<"ll:"<<std::stoll(s)<<std::endl<<"ul:"<<std::stoul(s)<<std::endl;
#else
std::cout<<"No support for stoll/stoul."<<std::endl;
#endif
return(0);
}

Output on Nexus 4 (Android 4.3):

u0_a51@mako:/ $ /data/local/tmp/cxx11
__GXX_EXPERIMENTAL_CXX0X__ defined.
_GLIBCXX_USE_C99 not defined.
_GLIBCXX_HAVE_BROKEN_VSWPRINTF not defined.
No support for stoll/stoul.

Android ndk std::to_string support

You can try LOCAL_CFLAGS := -std=c++11, but note that not all C++11 APIs are available with the NDK's gnustl. Full C++14 support is available with libc++ (APP_STL := c++_shared).

The alternative is to implement it yourself.

#include <string>
#include <sstream>

template <typename T>
std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}

int main()
{
std::string perfect = to_string(5) ;
}

using c++11 with ndk

This is https://github.com/android-ndk/ndk/issues/82

The fix for this is to switch from gnustl to libc++, but note that libc++ isn't as stable as gnustl is yet (working on fixing this ASAP, should be ready by the time r15 hits stable).

EDIT: As of NDK r16 libc++ is the recommended STL. Switch to using libc++ (see our docs) for full C++11 (and beyond) support.

Enable C++11 support on Android

First of all, you will need to ensure that your toolchain is "Cross GCC". Whilst it was the default on my Linux, it was not on my MacOSX Lion.

In order to do this, go to Project Properties > C/C++ Build > Tool Chain Editor. "Current toolchain" should be set to "Cross GCC". You might need to uncheck the box "Display compatible toolchains only".

Then, add an option to LOCAL_CFLAGS in Android.mk:

LOCAL_CFLAGS := -std=gnu++11

We now need to inform Eclipse about where to find the corresponding new symbols (e.g. "std::unordered_map"). Go to Right Click on "jni" > Properties > C/C++ General -> Paths and Symbols -> Symbols -> GNU C++, and add the following symbol (by clicking "Add..."):

Name: __GXX_EXPERIMENTAL_CXX0X__
Value:

(i.e. let "Value" empty)

Warning on using std::strtoul to convert std::string to unsigned short. Any better option available?

The problem is with the C-style cast (they are kinda risky). Use static_cast instead. Like:

u_var = static_cast< unsigned short >( std::strtoul(str.c_str(), NULL, 0) );

I'd also use std::stoul instead of std::strtoul, if it's possible (it's C++11)

No Member named stoi in namespace std

First of all, you need a compiler that supports C++11 and you need to compile in "C++11 mode" (in some cases).

Secondly, if this is in fact an intellisense issue (and it looks like it may be), then it could simply be that your IDE doesn't support C++11 yet.



Related Topics



Leave a reply



Submit