Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 std::async in Android NDK does not work

I have tried to get following sample code working to know whether asynchronous programming is working in Android NDK. Though NDK has the STL <future> which gets recognized as a header, the std::async is not getting recognized are not getting recognized. The code I tried to use was the following:

#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";
}

Also all the include paths have been set to the specified folder under 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
like image 298
user2508941 Avatar asked Jul 12 '13 14:07

user2508941


1 Answers

Curently Android NDK does not incorporate all of the C++11 features. Clang 3.3 compiler from NDK r9b is C++11-feature complete, however, STL and stdlib on Android are not.

To use the most recent C++11 feature set in Android use Clang 3.3 compiler from Android NDK r9b. Put this line into your Application.mk file:

NDK_TOOLCHAIN_VERSION := clang

Also, add -std=c++11 switch to the LOCAL_CPPFLAGS variable:

LOCAL_CPPFLAGS += -std=c++11
like image 176
Sergey K. Avatar answered Nov 09 '22 21:11

Sergey K.