【问题标题】:Android NDK and pthreadAndroid NDK 和 pthread
【发布时间】:2015-08-28 09:15:43
【问题描述】:

我正在使用 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?

【问题讨论】:

  • 正如heremanual 中所指出的: 这些函数是非标准的GNU 扩展;因此名称中的后缀“_np”(不可移植)。 Bionic 显然不支持它(另请参阅包含 Bionic C 库概述 的 NDK 文档,其中包含有关 pthread 实现和限制的精确度)。
  • @deltheil 说的是真的。我的解决方案是我不使用 pthread 功能。我通过条件编译达到了这一点,因为在 Android 系统上我无论如何都不使用线程功能。

标签: android c++ qt android-ndk pthreads


【解决方案1】:

请参阅https://android.googlesource.com/platform/bionic/+/master/docs/status.md,了解我们的官方文档,了解哪个 Android 版本是什么。

您还可以查看 NDK(当前版本 here)中的 <pthread.h> 标头,并查看以下示例条目:

pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);

这表明我们确实有非 POSIX/不可移植 (_np) 函数 pthread_gettid_np,但它是在 API 级别 21 中引入的,所以如果您的代码需要在旧版本上运行,您可以'不要使用它。

基本上,标头是“哪些函数在哪些 API 级别可用?”的规范事实来源。

对于pthread_getaffinity_np 的具体情况,不,我们不支持。你可以将pthread_gettid_np<pthread.h>sched_getaffinity<sched.h> 结合起来。

【讨论】:

    【解决方案2】:

    Is pthread functionality for Android limited?

    AFAIK,是的。

    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.
    

    【讨论】:

    • 链接已失效。
    • 链接已失效。我也面临着类似的问题。请帮忙。
    【解决方案3】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2012-03-19
      相关资源
      最近更新 更多