【问题标题】:wstring support in Android NDK applicationAndroid NDK 应用程序中的 wstring 支持
【发布时间】:2014-08-26 12:44:23
【问题描述】:

在我的 android NDK 应用程序中,我可以定义 wstring str = L"my string"。它编译和链接很好。

当我使用 __android_log_print(ANDROID_LOG_DEBUG, "tag", "JNI got initialized ...%S", str);

这是打印 JNI got initialized ...S,但我希望打印“JNI got initialized ...my string”

注意:我的 Application.mk 中有 APP_STL: = gnustl_static

提前问好。

【问题讨论】:

  • 尝试 %s 而不是 %S
  • 是的,但我需要传递 WSTRING,如果我使用 %s 的浪费,它只会给出第一个字符。
  • 基本上这是一个简单的 sn-p,我的 NDK 模块需要大量 wstring 支持。这是我面临的主要问题。

标签: android c++ linux android-ndk


【解决方案1】:

我发现了问题。基本上 wcstombs() API 在 android NDK 中不起作用。我编写了自己的 API 并解决了我的问题。

【讨论】:

    【解决方案2】:

    您的代码中有 2 个错误:

    1. %S 格式说明符是非标准扩展(来自 SUSv2)。它指示printf 将参数解释为wchar_t*
    2. 您传递的是std::wstring 对象而不是字符串指针。这是未定义的行为。

    如果你知道你的平台支持%S,并且你想使用这个非标准的扩展,你只需要修复你的参数传递:

    __android_log_print(ANDROID_LOG_DEBUG, "tag", "JNI got initialized ...%S", str.c_str());
    

    如果您想保持代码标准符合,您必须使用%s 并使用std::string,或者在调用__android_log_print 之前从std::wstring 转换为std::string。请注意,您仍然需要传递一个字符串指针(即str.c_str())。


    如果由于wcstombs 在Android 上产生无效输出,上述方法未产生预期结果,请参阅this Q&A 以获取解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 2014-05-11
      • 2015-08-26
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2014-12-25
      • 2015-04-27
      相关资源
      最近更新 更多