Android ndk std:: soporte para cadenas


Estoy usando android NDK r9d y toolchain 4.8 pero no puedo usar la función std::to_string, el compilador arroja este error:

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

¿Esta función no es compatible con Android ndk? Lo intento APP_CPPFLAGS := -std=c++11 sin suerte.

Author: Deduplicator, 2014-04-01

6 answers

Puede probar LOCAL_CFLAGS := -std=c++11, pero tenga en cuenta que no todas las API de C++11 están disponibles con gnustl del NDK. El soporte completo para C++14 está disponible con libc++ (APP_STL := c++_shared).

La alternativa es implementarlo usted mismo.

#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) ;
}
 59
Author: yushulx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-30 21:53:06

Con NDK r9+ puede usar llvm-libc++ que ofrece soporte completo para cpp11.

En su Application.mk hay que añadir:

APP_STL:=c++_static 

O

APP_STL:=c++_shared
 26
Author: thursdaysDove,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-12 03:45:16

Gradle

Si está buscando una solución para el sistema de compilación de Gradle. Mira esta respuesta.

Respuesta Corta.

Añade la cadena

arguments "-DANDROID_STL=c++_shared"

En su build.gradle. Como

android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {
      cmake {
        ...
        arguments "-DANDROID_STL=c++_shared"
      }
    }
  }
  ...
}
 8
Author: kyb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:34:42

Plugin experimental de Gradle

Si estás buscando una solución para el plugin Experimental de Gradle, esto funcionó para mí...

Probado con com.androide.herramienta.estructura: gradle-experimental:0.9.1

model {
  ...
  android {
    ...
    ndk {
      ...
      stl = "c++_shared"
    }
  }
}
 1
Author: spanndemic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-16 02:30:14

No pude usar c++_static, dio algunos errores sobre excepciones indefinidas. Así que volví a la gnustl_static.

Pero en las fuentes NDK, en sources/cxx-stl/llvm-libc++/src/string.cpp, encontré la implementación de to_string(int) y traté de copiarla a mi código. Después de algunas correcciones funcionó.

Así que la pieza final de código que tenía:

#include <string>
#include <algorithm>

using namespace std;


template<typename S, typename P, typename V >
inline
S
as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
{
    typedef typename S::size_type size_type;
    size_type available = s.size();
    while (true)
    {
        int status = sprintf_like(&s[0], available + 1, fmt, a);
        if ( status >= 0 )
        {
            size_type used = static_cast<size_type>(status);
            if ( used <= available )
            {
                s.resize( used );
                break;
            }
            available = used; // Assume this is advice of how much space we need.
        }
        else
            available = available * 2 + 1;
        s.resize(available);
    }
    return s;
}

template <class S, class V, bool = is_floating_point<V>::value>
struct initial_string;

template <class V, bool b>
struct initial_string<string, V, b>
{
    string
    operator()() const
    {
        string s;
        s.resize(s.capacity());
        return s;
    }
};

template <class V>
struct initial_string<wstring, V, false>
{
    wstring
    operator()() const
    {
        const size_t n = (numeric_limits<unsigned long long>::digits / 3)
          + ((numeric_limits<unsigned long long>::digits % 3) != 0)
          + 1;
        wstring s(n, wchar_t());
        s.resize(s.capacity());
        return s;
    }
};

template <class V>
struct initial_string<wstring, V, true>
{
    wstring
    operator()() const
    {
        wstring s(20, wchar_t());
        s.resize(s.capacity());
        return s;
    }
};

string to_string(int val)
{
    return as_string(snprintf, initial_string<string, int>()(), "%d", val);
}
 0
Author: mortalis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-25 21:09:31

Para Android Studio, agrega esto en build.gradle (Aplicación móvil)

externalNativeBuild {
    cmake {
        cppFlags "-std=c++11"

        arguments "-DANDROID_STL=c++_static"
    }
}
 0
Author: Expert Ngobeni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-22 09:58:16