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

13,038

Solution 1

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.

Solution 2

Adding:

APP_STL := c++_static

to Application.mk fixed my issue of not being able to access std::stoi() (using gcc 4.8.4).

solution via Daniel Tavares, from this Google Groups post.

Share:
13,038

Related videos on Youtube

Sergey K.
Author by

Sergey K.

I am a software developer originally from St.Petersburg, Russia with solid experience in mobile applications and 3D graphics. Rendering Lead at Ubisoft RedLynx. Head of Core Technology at Layar/Blippar/TWNKLS in the past. Coordinator and developer of http://www.linderdaum.com Coauthor of 3D Graphics Rendering Cookbook, Android NDK Game Development Cookbook and Mastering Android NDK CV: https://www.linkedin.com/in/sergey-kosarevsky/ Stack Overflow-milestones: 109th to the Research Assistant badge My blog: http://blog.linderdaum.com My GitHub: https://github.com/corporateshark

Updated on June 06, 2022

Comments

  • Sergey K.
    Sergey K. almost 2 years

    C++11 has two new string conversion functions into unsigned long and long long: std::stoul() and std::stoll().

    The recent Android NDK r9 introduces Clang 3.3 compiler which is said to be C++11 feature complete. There are prototypes for these functions deep inside NDK, however I cannot use them.

    What do I need to do to use them?

    P.S. I already do LOCAL_CPPFLAGS += -std=c++11

    • FDinoff
      FDinoff almost 11 years
      Probably similar to this stackoverflow.com/questions/15616254/…. but instead use clangs c++11 library. (I think its -stdlib=libc++ -std=c++11 for the LOCAL_CFLAGS)
    • Sergey K.
      Sergey K. almost 11 years
      I already do -std=c++11
    • aaronman
      aaronman almost 11 years
      -stdlib=libc++ is also needed make sure you try it
    • Sergey K.
      Sergey K. almost 11 years
      Added -stdlib=libc++ and defined GXX_EXPERIMENTAL_CXX0X Still no success.
    • FDinoff
      FDinoff almost 11 years
      I have no idea. I can't get this to work. I think some define is missing but I can't tell which. If you do a -E to show the preprocessor output you can see that bits/basic_string.h is included and that appears to be where stoll is defined however stoll does not appear in the preprocessor output.
  • Sergey K.
    Sergey K. almost 11 years
    Any chance to replace stdlib?
  • Samveen
    Samveen almost 11 years
    @SergeyK. Try CrystaX's Android NDK, which seems to have some fixes over the Vanilla Android NDK. I am looking to explore it as well.
  • Sergey K.
    Sergey K. almost 11 years
    Have you tried NDK r9? Maybe we can define _GLIBCXX_USE_C99 manually?
  • Samveen
    Samveen almost 11 years
    @SergeyK. Naah. Already tried LOCAL_CFLAGS := -D _GLIBCXX_USE_C99=1. It's a very deep-rooted problem.
  • Sergey K.
    Sergey K. almost 11 years
    Ok, this seems to be a great answer.
  • Samveen
    Samveen over 9 years
    @Leco As I mentioned "Possible fix" section in the answer and in the comment to SergeyK, take a look at CrystaX's Android NDK, which is a rebuild of the standard NDK with full C++11 support
  • Luis
    Luis over 9 years
    Thanks. I only needed std:stol so I copied the implementation from source and used it in my project.
  • Samveen
    Samveen over 8 years
    Did this also fix the issue of access to the unsigned long and long long variants of the functions(stoul and stoll)?
  • Stranger
    Stranger about 7 years
    That solution helped me with stoul(). Thanks.
  • Dan Albert
    Dan Albert almost 6 years
    For anyone landing here years later, the current fix is to switch to libc++ (APP_STL := c++_shared if you're using ndk-build). It doesn't have these problems.
  • Samveen
    Samveen almost 6 years
    @DanAlbert please post a solution instead of this comment. Better for all that way.
  • Samveen
    Samveen almost 6 years
    @DanAlbert the question is about std::stoul while the answer by Mark S. is about std::stoi.
  • Dan Albert
    Dan Albert almost 6 years
    It's the same problem.