using C++11 with Android native AOSP code

821 views
Skip to first unread message

Neta Zmora

unread,
Mar 18, 2014, 6:52:19 PM3/18/14
to android-...@googlegroups.com

Hi!

I’ve been trying to use std::function (which requires C++11) in an AOSP module (note: this is _not_ NDK code).

I am able to define and use lambda functions, so I know that C++11 is enabled, but I get an error saying that ‘function’ is not defined as part of the std namespace, although the compiler does not complain about not finding the <functional> header file.

If anyone’s used std::function with Android native code, I’d appreciate some advice on what I’m doing wrong.

I’m using stlport and I suspect it doesn't support std::function yet.  I can’t get the GCC STL to work, so help with that is also appreciated.

 

My code and Android.mk are pasted below.

Thanks!

Neta

 

main.cpp

----------------------------------------------

#include <functional>

#include <iostream>

using namespace std;

 

void print_num(int i) {

std::cout << i << '\n';

}

 

// This does not compile  ç

//std::function<void(int)> f_display = print_num;

 

template <typename T>

void use_f(T f) {

f(5);

}

 

int main(int argc, char** argv)

{

        use_f( [](int i){ std::cout << i << "\n";} );   // ç using lambda function

}

 

Android.mk

----------------------------------------------

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

 

LOCAL_SRC_FILES := main.cpp

 

LOCAL_SHARED_LIBRARIES := libutils

 

LOCAL_CPPFLAGS += -std=c++11

LOCAL_MODULE := neta_test

 

# Including this will modify the include path

include external/stlport/libstlport.mk

 

include $(BUILD_EXECUTABLE)

Liu Xin

unread,
Mar 19, 2014, 3:43:00 AM3/19/14
to android-...@googlegroups.com
In your code, what you are using is C++11 lamda. It's not related to library. please check out your compiler. i think gcc 4.5+ is good enough for lamda expression.
further, i don't think stlport is good choice if you want to make use of c++11 library features. You should consider libgnustl or libc++ in llvm. 

thanks,
--lx



--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.

Neta Zmora

unread,
Mar 21, 2014, 11:44:13 AM3/21/14
to android-...@googlegroups.com
Hi lx,

Perhaps I wasn't clear enough in my question: the c++11 lambda expression compiles fine.  It is the use of std::function, which is an STL template class, that does not compile.  This is because the AOSP uses an incomplete STLport

Thanks
Neta.
Reply all
Reply to author
Forward
0 new messages