0

This code will give an error message "to_string was not declared in this scope"

#include <jni.h>
#inlcude <string>
#include <android/log.h>
using namespace std;

JNIEXPORT jboolean JNICALL Java_com_package_activity_method
(JNIEnv *env, jobject classObject,jdouble number) {
  String string= to_string(number);
   __android_log_print(ANDROID_LOG_DEBUG, "JNI","The number is: %s,string);
  return;
}

if trace the header file I can find these in string.h -> basic_string.h

inline string
to_string(double __val)
{
  const int __n = 
   __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
                   "%f", __val);
}

So it was declared after all...

I can fix the problem by writing my own version of to_string

String to_string(double number){
    std::stringstream ss;
    ss << number;
    return ss.str();  }

or change -DANDROID_STL to c++_static

but what I am trying to understand it which part of the code obscure the to_string method in gnustl from being seen.

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
shadow_wxh
  • 366
  • 3
  • 17
  • Don't forget that it's in the `std` namespace. Perhaps you need to do `std::to_string`? – Some programmer dude Jul 05 '19 at 05:59
  • corrected but that is not the problem – shadow_wxh Jul 05 '19 at 06:28
  • Can you please edit your question to include a copy-paste (as text) of the *complete* and *full* error output? Including any possible informational notes. – Some programmer dude Jul 05 '19 at 06:30
  • /home/package/app/src/main/cpp/method.cpp: In function 'jboolean Java_com_package_activity_method(JNIEnv*, jobject, jdouble)': /home/package/app/src/main/cpp/method.cpp:67:36: error: 'to_string' was not declared in this scope String string= to_string(number); – shadow_wxh Jul 05 '19 at 07:16
  • `std::to_string` simply isn't available in the gnustl version that came with the NDK (see https://github.com/android-ndk/ndk/issues/82). And since gnustl has been deprecated since NDK r17 and removed altogether since NDK r18 it will never be added. You should be using libc++ instead (i.e. c++_static or c++_shared). – Michael Jul 05 '19 at 08:00
  • As for why the function is not visible to your code, see https://stackoverflow.com/questions/34517938/glibcxx-use-c99-is-not-defined-to-wstring-not-available – Michael Jul 05 '19 at 08:07
  • there are other compiled libraries still rely on gnustl before I convert all of them still I have to deal with it. – shadow_wxh Jul 05 '19 at 09:48

1 Answers1

0

from what I understand from the other post this is why to_string is not include in GNUSTL

in c++config.h line 1240 the following line is commented out and therefore _GLIBCXX_USE_C99 is not defined

/* 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 */

and this condition is checked at basic_string.h :2812

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

but why is it dong that I don't understand aren't stdio stdlib are some of the basic c libraries?

shadow_wxh
  • 366
  • 3
  • 17
  • _"but why is it dong that"_ Read the answer to the question I linked to in one of my comments on your question. – Michael Jul 05 '19 at 10:35