error: 'to_string' is not a member of 'std'

11,388

Solution 1

I ran into this issue too. to_string is not available in gnu-libstdc++ "out of the box". I searched the sources and found that std::to_string is indeed in the lib (sources/cxx-stl/gnu-libstdc++/4.9/include/bits/basic_string.h) but opted out by

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

After adding -D_GLIBCXX_USE_C99 to the build std::to_string is opted in.

Solution 2

Android NDK 9+ comes with llvm-libc++ which has full support for cpp11 features. To enable it, all you have to do is modify these in Application.mk:

APP_CPPFLAGS := -std=c++11

and

APP_STL:=c++_static 

or

APP_STL:=c++_shared

Solution 3

You may try this:

#include <string>
#include <sstream>

#if defined(__ANDROID__)
#define TO_STRING to_stringAndroid

template <typename T>
inline std::string to_stringAndroid(const T& value)
{
    std::ostringstream os ;
    os << value ;
    return os.str() ;
}


#else

#define TO_STRING std::to_string

#endif
Share:
11,388
Rafael Ruiz Muñoz
Author by

Rafael Ruiz Muñoz

Mathematician Android developer (Java/Kotlin, NDK) Flutter developer (Dart) Eventually iOS developer (Obj-C / Swift) Image processing (OpenCV - C++ / Python). Somehow involved with NodeJS &amp; MongoDB projects Passion for Machine Learning and Data Science (Python, Neural Networks, Tensorflow, Caffe) Passion for Audio processing (Python / MATLAB) Problem solver, logic flowing all the time!

Updated on July 30, 2022

Comments

  • Rafael Ruiz Muñoz
    Rafael Ruiz Muñoz almost 2 years

    It could be duplicate, but I tried all the solutions that I found on and out StackOverflow.

    I'm making a library on C++ with OpenCV and trying to compile it for Android.

    I can't use to_string(int)but I'm not able. I tried to modify my makefile too many times. My last configuration is this on:

    Android.mk

    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)

    OPENCV_CAMERA_MODULES:=off

    OPENCV_INSTALL_MODULES:=on

    include $(LOCAL_PATH)/jsoncpp/Android.mk

    include /Users/rafaelruizmunoz/Desktop/AndroidDevelopment/OpenCV-2.4.9-android- sdk/sdk/native/jni/OpenCV.mk

    OPENCV_LIB_TYPE:=SHARED

    LOCAL_C_INCLUDES += $(LOCAL_PATH)

    LOCAL_C_INCLUDES += /Users/rafaelruizmunoz/opencvscan/OpenCVtry/

    LOCAL_C_INCLUDES += /Users/rafaelruizmunoz/Desktop/RD/OpenCVtry/Libraries/jsoncpp- master/include

    LOCAL_PATH := jni

    LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

    LOCAL_MODULE := libXYZ

    LOCAL_MODULE_NAME := mylibXYZ

    LOCAL_SRC_FILES := androidClass.cpp main.cpp utils.cpp

    LOCAL_LDLIBS += -llog -ldl

    LOCAL_CPPFLAGS := -std=c++11 CFLAGS=-g -Wall -Wextra -std=c++11 -Wno-write-strings ../../include/boost

    LOCAL_SHARED_LIBRARIES := libJsoncpp libopencv_java

    include $(BUILD_SHARED_LIBRARY)

    and this is my Application.mk

    APP_STL := gnustl_static

    APP_CPPFLAGS := -frtti -fexceptions

    APP_ABI := all

    APP_MODULES := libXYZ libJsoncpp

    APP_CPPFLAGS := -std=gnu++0x

    APP_CPPFLAGS += -frtti

    APP_CPPFLAGS += -fexceptions

    APP_CPPFLAGS += -DDEBUG

    APP_CPPFLAGS += -std=c++11

    NDK_TOOLCHAIN_VERSION := 4.8

    LOCAL_C_INCLUDES += ${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/include

    APP_USE_CPP0X := true

    Thanks in advance.

  • Violet Giraffe
    Violet Giraffe about 8 years
    This stdlib is compatible with GCC, not only with clang, is it?
  • Dmytro
    Dmytro about 5 years
    Better to pass parameter as reference: inline std::string to_stringAndroid(const T& value)