Tôi cố gắng để biên dịch chương trình đơn giản này với android-ndk-r8b
:
JNI/hello_jni.cppCách sử dụng chuỗi C++ 0x trong Android NDK?
#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 trả về cho tôi một lỗi cho rằng 'thread' không phải là thành viên của 'std'. I ban hành ndk-xây dựng -n để có được lệnh biên soạn và phát hành nó một mình trong vỏ của tôi:
/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
Tôi đọc rất nhiều bài/câu hỏi về chủ đề POSIX và đề C++, nhưng vẫn không thể tìm thấy tôi câu trả lời. tập tin arm-linux-androideabi/include/c++/4.6/thread
tôi định nghĩa class thread
chỉ std
:
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
Họ dường như không được định nghĩa trong sdk của tôi (C++ config.h). Nhưng làm thế nào tôi có thể biến chúng một cách an toàn? Tôi có cần phải biên dịch toolchain của riêng tôi để sử dụng (không-p) đề? Máy tính lưu trữ của tôi là:
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
Cảm ơn Sergey. Tại sao bộ android đưa vào/C++/4.6/header trong NDK?Làm thế nào để bạn thực hiện một số công cụ đa luồng với NDK trong Android mà không cần? Bạn có sử dụng các chủ đề POSIX không? –
Tôi sử dụng chủ đề POSIX trong Android. Họ làm việc tốt. Tôi không có ý tưởng về các tiêu đề mới trong chuỗi công cụ 4.6. –
Ok cảm ơn. @ commoncpp @ cung cấp lớp Thread, tôi sẽ thử điều đó. –