如何使用c ++ 0x线程在Android NDK? [英] How to use c++0x thread in Android NDK?

查看:239
本文介绍了如何使用c ++ 0x线程在Android NDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 android-ndk-r8b 编译这个简单的程序:

jni / hello_jni.cpp

  #include< iostream> 
#include< thread>

void hello()
{
std :: cout< 你好我是一个线程!!! << 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

$包括(BUILD_EXECUTABLE)

ndk-build 返回一个错误,认为'thread'不是成员的'std'。
我发出 ndk-build -n 以获得编译命令并在我的shell中单独发出:

  /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 /本地/ armeabi / OBJ文件/ hello_thread / hello_thread.od -fpic -ffunction截面-funwind桌-fstack保护器-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 /包括-I /家庭/ evigier / Android的NDK-R8B /来源/ CXX-STL / GNU-的libstdc ++ / 4.6 /库/ armeabi /包括-I /家庭/ 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 
++编译大拇指:hello_thread< = hello_thread.cpp
在/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/stdio.h:55:0中包含的文件中,
从/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/wchar.h:33,
来自/ home / evigier / android-ndk-r8b / sources / cxx -stl / gnu-libstdc ++ / 4.6 / include / cwchar:46,
从/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/postypes.h :42,
从/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iosfwd:42,从/家/ evigier / Android的NDK
-r8b / sources / cxx-stl / gnu-libstdc ++ / 4.6 / include / ios:39,
来自/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include / ostream的:40,从/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iostream:40
,从JNI / hello_thread.cpp
: 4:
/home/evigier/android-ndk-r8b/platforms/android-14/arch-man/usr/include/sys/types.h:124:9:错误:'uint64_t'不命名类型
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:在函数'廉政的main()':
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:14: 5:错误:'thread'不是'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:错误:'th'未在此范围中声明

我读了很多关于POSIX线程和C ++线程的线程/问题,但仍然找不到我的答案。我的 arm-linux-androideabi / include / c ++ / 4.6 / thread 文件定义类线程 > std only:

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

它们似乎没有在我的sdk H)。但我怎么可能安全地把他们?我需要编译自己的工具链使用(非P)线程吗?我的主机是:

  Linux evigier- ThinkPad X220 3.0.0-17-通用#30- Ubuntu SMP 3月8日20:45:39 UTC 2012 x86_64 x86_64 x86_64 GNU / Linux 


解决方案

p> Android NDK r8b自带gcc 4.6,不包含任何C ++ 11线程的实现。你必须提供自己的实现,并可能建立自己独立的gcc工具



检查这些网页中的gcc C ++ 11的支持:


  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 code>调用。


I am trying to compile this simple program with android-ndk-r8b:
jni/hello_jni.cpp

#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 returns me an error arguin that 'thread' is not a member of 'std'. I issued ndk-build -n to get the compilation command and issued it alone in my shell:

/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

I read a lot of threads/questions about POSIX threads and C++ threads, but still cannot find my answer. My arm-linux-androideabi/include/c++/4.6/thread file defines class thread in std only:

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

They don't seem to be defined in my sdk (c++config.h). But how can I possibly turn them on safely? Do i need to compile my own toolchain to use (non-p)threads? My host computer is :

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

解决方案

Android NDK r8b comes with gcc 4.6 and does not contain any implementation of C++11 threads. You will have to provide your own implementations and possibly build your own standalone gcc toolchain.

Check these pages for C++11 support in gcc:

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

POSIX threads is a completely different story and you can use them in Android NDK. You will need at least pthread_create(), pthread_detach() and pthread_join() calls to do it.

这篇关于如何使用c ++ 0x线程在Android NDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆