使用 ADT/Eclipse 为 NDK 设置 C++11(std::thread)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23911019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:43:13  来源:igfitidea点击:

Setting up C++11 (std::thread) for NDK with ADT/Eclipse

androideclipsec++11android-ndkjava-native-interface

提问by Wajih

I have been trying to use C++11. I am developing an android project and i want to use std::mutex. Along with OpenCV But no matter what I do, I just cant seem to fix the Type 'mutex' could not be resolvederror.

我一直在尝试使用 C++11。我正在开发一个 android 项目,我想使用 std::mutex。与 OpenCV 一起但无论我做什么,我似乎都无法修复Type 'mutex' could not be resolved错误。

I have tried following the tutorials i found on SO and other places. LINK1LINK2LINK3LINK4

我尝试按照我在 SO 和其他地方找到的教程进行操作。 LINK1 LINK2 LINK3 LINK4

  1. ADT v22.3.0-887826
  2. Installed C/C++ compilers (CDT)
  1. ADT v22.3.0-887826
  2. 已安装的 C/C++ 编译器 (CDT)

Following so many tutorials, it has become a real mess now. So I will explain my current settings

看了这么多教程,现在变得一团糟。所以我将解释我当前的设置

  • Project > Properties > C/C++ Build > Tool Chain Editor
    • Current Tool Chain is "Cross GCC"
    • Current Builder is "Android Builder"
  • Project > Properties > C/C++ Build > Discovery Options

    • Compiler invocation command is "gcc"
    • Compilter Invocation argments are -E -P -v -dD "${plugin_state_location}/specs.c -std=c++11"
  • Project > C/C++ General > Paths and Symbols > # Symbols tab

    • Symbol = __cplusplus and Value = 1
  • 项目 > 属性 > C/C++ 构建 > 工具链编辑器
    • 当前的工具链是“Cross GCC”
    • 当前的生成器是“Android 生成器”
  • 项目 > 属性 > C/C++ 构建 > 发现选项

    • 编译器调用命令是“gcc”
    • 编译器调用参数是 -E -P -v -dD "${plugin_state_location}/specs.c -std=c++11"
  • 项目 > C/C++ 常规 > 路径和符号 > # 符号选项卡

    • 符号 = __cplusplus 和值 = 1

In my Application.mk file I have the following

在我的 Application.mk 文件中,我有以下内容

APP_STL := gnustl_static
APP_USE_CPP0X := true
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8

I tried to change the cplusplus symbol's value to 201103L and tried __GXX_EXPERIMENTAL_CXX0Xwith an empty value

我尝试将cplusplus 符号的值更改为 201103L 并尝试使用空值__GXX_EXPERIMENTAL_CXX0X

But nothing seems to work, What am I doing wrong??

但似乎没有任何效果,我做错了什么?

Any help is appreciated!

任何帮助表示赞赏!

回答by Alex Cohn

Support for std::threadis a bit special. The issue is addressed, for example, in this article by Binglong. The article is really short, but it can be summarized in one sentence:

支持std::thread有点特殊。例如,Binglong这篇文章中解决了这个问题。文章真的很短,但可以用一句话概括:

You cannot use the (default) gcc 4.6 toolchain if you want to #include <thread>or #include <mutex>.

如果需要,您不能使用(默认)gcc 4.6 工具链#include <thread>#include <mutex>

So, please add NDK_TOOLCHAIN_VERSION=4.8or NDK_TOOLCHAIN_VERSION=clangto your Application.mk.

因此,请将NDK_TOOLCHAIN_VERSION=4.8或添加NDK_TOOLCHAIN_VERSION=clang到您的Application.mk.

For ADT to rebuild its Indexcorrectly, see Android NDK build, Method could not be resolvedor Eclipse compiles successfully but still gives semantic errors.

为了让 ADT正确重建其索引,请参阅 Android NDK 构建,方法无法解析Eclipse 编译成功但仍然给出语义错误

回答by Aqua

In Android.mkadd LOCAL_CPPFLAGS := -std=c++11 -D __cplusplus=201103Lthen rebuild your project (for reconfiguring compiler). After rebuilding, your project automatically adds needed stl path into Path and Symbols

Android.mk添加LOCAL_CPPFLAGS := -std=c++11 -D __cplusplus=201103L然后重建您的项目(用于重新配置编译器)。重建后,您的项目会自动将所需的stl路径添加到Path和Symbols中

回答by Dave the Irritated One

If you upgrade the NDK or install a fresh version of Android Studio (2.1 at time of writing) and have Android Studio download the NDK for you you will get revision 12 - which has a lot of the std:: defines not defined in \ndk-bundle\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a\include\bits\c++config.h - ones relevant for threading being such as _GLIBCXX_HAS_GTHREADS which hides the thread class in "thread" for example.

如果您升级 NDK 或安装新版本的 Android Studio(在撰写本文时为 2.1)并让 Android Studio 为您下载 NDK,您将获得修订版 12 - 其中包含许多未在 \ndk 中定义的 std:: 定义-bundle\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a\include\bits\c++config.h - 与线程相关的,例如 _GLIBCXX_HAS_GTHREADS 将线程类隐藏在“线程”中例子。

It says that after revision 10e that gcc is deprecated. And with this all the defines as mentioned - which thoroughly buggered our thread dependant JNI code.

它说在修订版 10e 之后不推荐使用 gcc。有了这个,所有提到的定义 - 这彻底破坏了我们依赖线程的 JNI 代码。

Clang as suggested in other posts is not a solution for us as it amongst other things seems not to support thead_local for example. The solution is to revert back to revision 10e - which you can find at:

其他帖子中建议的 Clang 不是我们的解决方案,因为它似乎不支持 thead_local 例如。解决方案是恢复到修订版 10e - 您可以在以下位置找到:

dl.google.com/android/ndk

dl.google.com/android/ndk

Extract the package and copy into the sdk/ndk-bundle directory - make sure you delete the original revision 12 first.

解压包并复制到 sdk/ndk-bundle 目录 - 确保先删除原始修订版 12。