2012-07-30 19 views
5

我试图编译这个简单程序与android-ndk-r8b
JNI/hello_jni.cpp如何在Android NDK中使用C++ 0x线程?

#include <iostream> 
#include <thread> 

void hello() 
{ 
    std::cout << "Hi i'm a thread!!!" << std::endl; 
} 

int main() 
{ 
    std::thread th(hello); 
    th.join(); 
    return 0; 
} 

JNI/Application.mk

APP_OPTIM := release 
APP_MODULES := hello_thread 
APP_STL := gnustl_static 

JNI/Android.mk

LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS) 

LOCAL_CPPFLAGS += -std=c++0x -frtti 

LOCAL_MODULE  := hello_thread 
LOCAL_LDLIBS  := -L$(SYSROOT)/usr/lib -pthread 
LOCAL_SRC_FILES := hello_thread.cpp 

include $(BUILD_EXECUTABLE) 

ndk-build给我一个错误,认为'线索'不是'std'的成员。 我发出NDK,建立-n使编译命令,并在我的壳单独发出它:

/home/evigier/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ -MMD -MP -MF /home/evigier/eclipse_workspace/hello_thread/obj/local/armeabi/objs/hello_thread/hello_thread.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include -I/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include -I/home/evigier/eclipse_workspace/hello_thread/jni -DANDROID -Wa,--noexecstack -std=c++0x -frtti -O2 -DNDEBUG -g -I/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include -c /home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp -o /home/evigier/eclipse_workspace/hello_thread/obj/local/armeabi/objs/hello_thread/hello_thread.o 
Compile++ thumb : hello_thread <= hello_thread.cpp 
In file included from /home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/stdio.h:55:0, 
       from /home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/wchar.h:33, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/cwchar:46, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/postypes.h:42, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iosfwd:42, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/ios:39, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/ostream:40, 
       from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iostream:40, 
       from jni/hello_thread.cpp:4: 
/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/sys/types.h:124:9: error: 'uint64_t' does not name a type 
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp: In function 'int main()': 
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:14:5: error: 'thread' is not a member of 'std' 
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:14:17: error: expected ';' before 'th' 
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:15:5: error: 'th' was not declared in this scope 

我读了很多关于POSIX线程和C++线程的线程/问题,但仍无法找到我回答。我arm-linux-androideabi/include/c++/4.6/thread文件中只std定义class thread

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) 

他们似乎并不在我的SDK被定义(C++的config.h)。但是我怎么能安全地把它们打开?我是否需要编译自己的工具链才能使用(非-p)线程?我的主机是:

Linux evigier-ThinkPad-X220 3.0.0-17-generiC#30-Ubuntu SMP Thu Mar 8 20:45:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux 

回答

6

的Android NDK R8B自带GCC 4.6,并且不包含C++ 11个线程任何实现。你将不得不提供你自己的实现,并可能建立你自己的独立gcc工具链。

检查这些网页C++ 11支持GCC:

  1. http://gcc.gnu.org/projects/cxx0x.html
  2. http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x

POSIX线程是一个完全不同的故事,你可以在Android的NDK中使用它们。您至少需要拨打pthread_create()pthread_detach()pthread_join()

+1

谢谢谢尔盖。那么为什么android系统会在NDK中包含include/C++/4.6/thread头文件呢?如何在没有的情况下在android中使用NDK来执行一些多线程内容?你使用POSIX线程吗? – 2012-07-31 13:27:23

+0

我在Android中使用POSIX线程。他们工作得很好。我不知道4.6工具链中的新标题。 – 2012-07-31 13:31:14

+0

好的谢谢。 @ commoncpp @提供了类Thread,我会试试这个。 – 2012-07-31 13:45:12

2

STL线程库的灵感来自于boost库,您可以为android操作系统编译该库。因此,编译Boost.Thread以获得替代但类似的实现。

+0

我没有使用Boost,但[commoncpp2-1.8.1](http://www.gnu.org/software/commoncpp),它具有线程实现功能。在Android ndk中构建它需要做一些更改。 – 2012-09-11 22:02:01

+1

你没有,但你可以,如果你想'boost :: thread'。近似的'std :: thread' – user1095108 2012-09-13 10:08:01

4

这里更新,因为这是对我的std :: this_thread问题上结果:

线程支持在最新R8E NDK版本进行了改进(2013年3月)。确保你使用的是最新的NDK和工具链,你的问题可能会被微不足道地解决。

编辑:版本更改简单地列为“在GCC/MIPS工具链中启用线程支持”。至少它增加了对std :: this_thread :: sleep_for的支持。如果其他人知道有关新支持范围的更详细的文档,请将其链接。

+0

+1有用的信息。你可以用他们刚刚添加的内容编辑你的答案吗? – 2013-03-27 18:09:44