Android NDK中的C++ 11 std :: async不起作用

use*_*941 6 android android-ndk c++11

我试图获取以下示例代码,以了解异步编程是否在Android NDK中有效.尽管NDK具有<future>被识别为标题的STL ,但std::async未被识别的STL 未被识别.我试图使用的代码如下:

#include <future>
#include <iostream>

struct Foo 
{
  Foo() : data(0) {}
  void sum(int i) { data +=i;}
  int data;
};

int main()
{
    Foo foo;
    auto f = std::async(&Foo::sum, &foo, 42);
    f.get();
    std::cout << foo.data << "\n";
}
Run Code Online (Sandbox Code Playgroud)

此外,所有包含路径都已设置为Properties-> Paths and Symbols下的指定文件夹

Errors
Description Resource    Path    Location    Type
invalid use of incomplete type 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Sample.cpp  /Project12/jni  line 50 C/C++ Problem

Description Resource    Path    Location    Type
declaration of 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Project12       line 111, external location: D:\android-ndk-r8e-windows-x86_64\android-ndk-r8e\sources\cxx-stl\gnu-libstdc++\4.6\include\future C/C++ Problem
Run Code Online (Sandbox Code Playgroud)

Ser*_* K. 9

当然,Android NDK并未包含所有C++ 11功能.来自NDK r9b的Clang 3.3编译器完全是C++ 11功能,STLstdlib在Android上却没有.

要使用C++11Android中最新的功能集,请使用Clang 3.3编译器Android NDK r9b.将此行放入您的Application.mk文件:

NDK_TOOLCHAIN_VERSION := clang
Run Code Online (Sandbox Code Playgroud)

另外,添加-std=c++11切换到LOCAL_CPPFLAGS变量:

LOCAL_CPPFLAGS += -std=c++11
Run Code Online (Sandbox Code Playgroud)

  • @ user2508941 NDK r9已经出局,铿锵3.3. (2认同)