【问题标题】:std::stoul errors out on a Qt app for android with NDK r13b带有 NDK r13b 的 Android Qt 应用程序出现 std::stoul 错误
【发布时间】:2017-01-04 15:55:30
【问题描述】:

Qt appandroid 上工作。我正在编写几乎是C++ 14 标准的代码。也可以使用 make_uniquemake_shared 等函数。

但是,当我尝试使用std::stoul 时,它只会在android 上编译失败。它在osxiOS 上为clang 编译和工作。适用于Microsoft compiler on windows。但只能在 Android 上编译失败,并显示以下错误:-

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

我的Qt app 使用最新的NDK r13b 编译android 的代码。

我搜索了很多,发现人们面临类似的问题,例如 herehere但是,我需要在Qt app 上解决此问题。对于android

我将以下内容添加到我的Qt App.pro 文件中。但是没用。

如何使用最新的NDK r13bstd::stoul 编译为androidQt app

还尝试将以下块添加到我的应用程序的 .pro 文件中。 但无济于事

android {
 QMAKE_CXXFLAGS_RELEASE += -std=c++1y
 QMAKE_CXXFLAGS_DEBUG += -std=c++1y
 CONFIG += c++14

 QMAKE_APP_FLAG +=c++_static
}

【问题讨论】:

  • 只要您使用 Qt,我建议您尽可能多地使用 QString。例如,您可以执行 QString(YOUR_STRING).toLong()。这里 YOUR_STRING 也可以是标准的 C++ 字符串。
  • 我在核心 C++ 类中编写此代码。此代码必须是严格的 C++。我没有那个QString 选项
  • 好的,您在 Android 中使用的是什么编译器?作为最后一个资源,如果 stoll 不可用,您可以尝试旧的良好 sscanf 函数,例如 sscanf(str.c_str(), "%ld, &yourInteger)。
  • 编译器为gcc,最新ndk提供的版本

标签: c++ qt c++11 android-ndk qmake


【解决方案1】:

解决此问题的最简单方法是将函数注入命名空间 std。请注意,您可能需要以更正确的方式实现这些,因为我只是在一些代码中添加了一些代码以使其看起来正确。所以创建一个你喜欢的新文件并添加:

#ifndef MYFILE_H
#define MYFILE_H

#if defined(__ANDROID__)
namespace std {
    unsigned long stoul(const std::string &str)
    {
        if (str.length() == 0) {
            return 0;
        }
        auto returnValue = atoi(str.c_str());
        if (returnValue == 0) {
            throw std::invalid_argument("stoul: invalid string " + str);
        }
        return returnValue;
    }
}
#endif

#endif //MYFILE_H

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2017-12-09
    • 2011-10-17
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多