Android NDK 和 pthread
Android NDK and pthread
我正在使用 android NDK 独立工具链编译 Qt/C++ 项目。我用 make-standalone-toolchain.sh --arch=arm --toolchain=arm-linux-androideabi-4.9 --platform=android 创建了独立的工具链-21 命令。 NDK 版本为 android-ndk-r10e。目标项目使用 pthread 库中的一些函数。在编译时,出现以下错误:
error: 'pthread_getaffinity_np' was not declared in this scope
const int err = pthread_getaffinity_np(_pthreadId, sizeof(cpu_set_t), &cpuSetMask);
compilation terminated due to -Wfatal-errors.
我检查了 ndk 工具链中包含的 pthread 的头文件,但没有找到 pthread_getaffinity_np 函数的声明。
Android 的 pthread 功能是否受限?如何正确使用带有 Android NDK 的 pthread?
Is pthread functionality for Android limited?
据我所知,是的。
http://mobilepearls.com/labs/native-android-api/#pthreads
https://web.archive.org/web/20180602101341/http://mobilepearls.com/labs/native-android-api/#pthreads
POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no
additional linking (-lpthreads) is necessary. It does not implement full
POSIX threads functionality and leaves out support for read/write locks,
pthread_cancel(), process-shared mutexes and condition variables as well as
other more advanced features. Read the bionic OVERVIEW.txt for more
information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available
to applications, lower than the posix minimum of 128.
POSIX 线程 (pthreads) 似乎没有为 -host 构建模块提供。
至少这里是 libcrypto-host 模块构建的错误:
out/host/linux-x86/obj/SHARED_LIBRARIES/libcrypto-host_intermediates/src/crypto/thread_pthread.o:
In function `thread_local_init':
/media/compilation/projects/android/beagle2/external/boringssl/src/crypto/thread_pthread.c:112:
undefined reference to `pthread_key_create'
到目前为止,唯一的修复方法是在里面添加 -lpthread
external/boringssl/Android.mk 指令前:
include $(BUILD_HOST_SHARED_LIBRARY)
示例:
# Host shared library
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libcrypto-host
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_MULTILIB := both
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk
LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -Wno-unused-parameter
LOCAL_CFLAGS += -DOPENSSL_NO_ASM
LOCAL_LDLIBS += -lpthread
include $(LOCAL_PATH)/crypto-sources.mk
include $(BUILD_HOST_SHARED_LIBRARY)
有关 Android 版本的官方文档,请参阅 https://android.googlesource.com/platform/bionic/+/master/docs/status.md。
您还可以查看 NDK(当前版本 here)中的 <pthread.h>
header,例如:
pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);
这表明我们确实有 non-POSIX/non-portable (_np
) 函数 pthread_gettid_np
,但它是在 API 级别 21 中引入的,所以如果您的代码需要运行 在旧版本上你不能使用它。
基本上 header 是 "which functions are available in which API levels?" 的规范真实来源。
对于 pthread_getaffinity_np
的具体情况,不,我们不支持。不过,您可以将 <pthread.h>
中的 pthread_gettid_np
和 <sched.h>
中的 sched_getaffinity
结合起来。
我正在使用 android NDK 独立工具链编译 Qt/C++ 项目。我用 make-standalone-toolchain.sh --arch=arm --toolchain=arm-linux-androideabi-4.9 --platform=android 创建了独立的工具链-21 命令。 NDK 版本为 android-ndk-r10e。目标项目使用 pthread 库中的一些函数。在编译时,出现以下错误:
error: 'pthread_getaffinity_np' was not declared in this scope
const int err = pthread_getaffinity_np(_pthreadId, sizeof(cpu_set_t), &cpuSetMask);
compilation terminated due to -Wfatal-errors.
我检查了 ndk 工具链中包含的 pthread 的头文件,但没有找到 pthread_getaffinity_np 函数的声明。
Android 的 pthread 功能是否受限?如何正确使用带有 Android NDK 的 pthread?
Is pthread functionality for Android limited?
据我所知,是的。
http://mobilepearls.com/labs/native-android-api/#pthreads
https://web.archive.org/web/20180602101341/http://mobilepearls.com/labs/native-android-api/#pthreads
POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no
additional linking (-lpthreads) is necessary. It does not implement full
POSIX threads functionality and leaves out support for read/write locks,
pthread_cancel(), process-shared mutexes and condition variables as well as
other more advanced features. Read the bionic OVERVIEW.txt for more
information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available
to applications, lower than the posix minimum of 128.
POSIX 线程 (pthreads) 似乎没有为 -host 构建模块提供。 至少这里是 libcrypto-host 模块构建的错误:
out/host/linux-x86/obj/SHARED_LIBRARIES/libcrypto-host_intermediates/src/crypto/thread_pthread.o:
In function `thread_local_init':
/media/compilation/projects/android/beagle2/external/boringssl/src/crypto/thread_pthread.c:112:
undefined reference to `pthread_key_create'
到目前为止,唯一的修复方法是在里面添加 -lpthread external/boringssl/Android.mk 指令前:
include $(BUILD_HOST_SHARED_LIBRARY)
示例:
# Host shared library
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libcrypto-host
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_MULTILIB := both
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk
LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -Wno-unused-parameter
LOCAL_CFLAGS += -DOPENSSL_NO_ASM
LOCAL_LDLIBS += -lpthread
include $(LOCAL_PATH)/crypto-sources.mk
include $(BUILD_HOST_SHARED_LIBRARY)
有关 Android 版本的官方文档,请参阅 https://android.googlesource.com/platform/bionic/+/master/docs/status.md。
您还可以查看 NDK(当前版本 here)中的 <pthread.h>
header,例如:
pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);
这表明我们确实有 non-POSIX/non-portable (_np
) 函数 pthread_gettid_np
,但它是在 API 级别 21 中引入的,所以如果您的代码需要运行 在旧版本上你不能使用它。
基本上 header 是 "which functions are available in which API levels?" 的规范真实来源。
对于 pthread_getaffinity_np
的具体情况,不,我们不支持。不过,您可以将 <pthread.h>
中的 pthread_gettid_np
和 <sched.h>
中的 sched_getaffinity
结合起来。