6

我正在尝试使用以下命令编译这个简单的程序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/应用程序.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向我返回一个错误,认为“线程”不是“标准”的成员。我发出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/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仅定义:class threadstd

#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
4

3 回答 3

7

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()调用来执行此操作。

于 2012-07-31T07:56:09.570 回答
5

在这里更新是因为这是我的 std::this_thread 问题的最佳结果:

最新的 r8e NDK 版本(2013 年 3 月)中改进了线程支持。确保您使用的是最新的 NDK 和工具链,您的问题可能会很容易解决。

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

于 2013-03-27T17:52:33.767 回答
2

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

于 2012-09-11T21:36:51.550 回答