8

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 resolved error.

I have tried following the tutorials i found on SO and other places. LINK1 LINK2 LINK3LINK4

  1. ADT v22.3.0-887826
  2. Installed C/C++ compilers (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

In my Application.mk file I have the following

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_CXX0X with an empty value

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

Any help is appreciated!

Community
  • 1
  • 1
Wajih
  • 793
  • 3
  • 14
  • 31
  • 1
    Is the problem simply an error highlighted by Eclipse, or does your build fail as well? Eclipse is buggy with C++11. – Steve May 28 '14 at 12:06
  • 1
    Possible duplicate of [Android NDK build, Method could not be resolved](http://stackoverflow.com/questions/23155676/android-ndk-build-method-could-not-be-resolved). Either way, you don't need CDT; you must install NDK though. – Alex Cohn May 28 '14 at 12:29
  • 2
    I don't think *"`__cplusplus` and Value = 1"* is correct; the value should be at least `201103L` for C++11 in my opinion. But I think you really shouldn't mess with the value `__cplusplus`. Now, what does *"But nothing seems to work"* mean? In short, please answer Steve's question. – Ali May 28 '14 at 13:15
  • 1
    One more thing: [Did you rebuild the index](http://stackoverflow.com/a/14135232/341970)? – Ali May 28 '14 at 13:17
  • well thanks for the replies. I have rebuilt the index. I have correct paths to NDK as well. I can build a simple opencv code with native support calling a single c++ function. – Wajih May 28 '14 at 14:47
  • @Steve - the build keeps failing. Does that mean it fails just because Eclipse is buggy? Is there a work around for this? I find it extremely frustrating. Two days and this problem does not go away – Wajih May 28 '14 at 14:48
  • @Ali as I mentioned, I did change the value from 1 to 201103L. And the same error persists. – Wajih May 28 '14 at 14:49
  • @Wajih Yes, I saw that, I am just saying it was a pointless attempt and perhaps you might as well remove it from the question. – Ali May 28 '14 at 14:53
  • Well let me check the answer as my question has been marked duplicate. – Wajih May 28 '14 at 14:56
  • I am so out of luck right now! The error is still there after following the answer – Wajih May 28 '14 at 15:08
  • 1
    What is the actual version of GCC you're using? If `__cplusplus` == 1, then it's quite likely too old a version. – ildjarn May 29 '14 at 00:48
  • The original title was misleading. The real problem had nothing to do with ADT, but rather with NDK. – Alex Cohn Jun 02 '14 at 08:39
  • 1
    If you found the answer, _post it as an answer_. – Lightness Races in Orbit Aug 10 '14 at 01:40
  • If you need to use non-GPL code due to contractual obligations, then see [Issue 216331: STLport does not support C++11](http://code.google.com/p/android/issues/detail?id=216331) in the AOSP bug tracker. – jww Jul 17 '16 at 23:00

3 Answers3

9

Support for std::thread is 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:

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

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

For ADT to rebuild its Index correctly, see Android NDK build, Method could not be resolved or Eclipse compiles successfully but still gives semantic errors.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • So what about the "include" sources where from tutorials I see cxx-stt/4.6 sort of stuff. Does this mean I have to change the include sources too? Probably yes, only then will your answer work, right? – Wajih Jun 02 '14 at 06:12
  • Let us split your problem in two. First of all, make sure that your **ndk-build** works. Second, work around the Eclipse indexer. For the second step, naturally, you want to substitute `4.6` in *[Android NDK build, Method could not be resolved](http://stackoverflow.com/questions/23155676/android-ndk-build-method-could-not-be-resolved)* by `4.8`. – Alex Cohn Jun 02 '14 at 08:34
  • Thanks for the reply. Well NDK does build under current settings which includes 4.6 as I am able to use it with OpenCV. So I will check on te 4.8 issue. I guess most people don't mention what needs to be included properly so that c++11 can be fully supported. Will post back once I go through the links you have mentioned. – Wajih Jun 02 '14 at 09:52
  • Well I have tried the settings now I was able to remove the error finally But still I am not sure if everything will work fine. I declared std::mutex, as I type i wont find the auto-completion feature show me 'mutex' as an option. I assume this is only an indexing problem? Take a look at my settings please do let me know if anything is wrong. http://postimg.org/image/z0j1ehvft/ – Wajih Jun 02 '14 at 18:54
  • No, the trick for the Indexer is to uncheck *compatible only* and choose *Linux GCC* toolchain. – Alex Cohn Jun 02 '14 at 21:12
  • But still, why would not a cross GCC compiler be good? I saw it as an option in a few posts. – Wajih Jun 03 '14 at 05:14
  • 1
    There are probably different workarounds for the problem with *Indexer*. Maybe your approach is good, too. – Alex Cohn Jun 03 '14 at 15:11
  • I could not find the "compatible only" option, may be eclipse or my brain is too messed up right now! – Wajih Jun 03 '14 at 18:13
  • Well reply to me where i can find the indexer option. Till then I am marking yours' as an answer and going to put that image there! – Wajih Jun 03 '14 at 18:30
  • 1
    Yes I have! Finally it has worked! Man was this so tough to nail. I had to restart eclipse, which took me 3 hours to figure out :) But all is working. – Wajih Jun 04 '14 at 10:09
  • Where is Android.mk in VisualStudio android project? – Michael IV Jun 13 '17 at 07:43
  • @MichaelIvanov as far as I know, MS Visual Studio does not create or use **Android.mk** file. Some while ago, their solution was using a normal NDK project with **Android.mk** under the hood (see https://visualgdb.com/tutorials/android/) but today *[a bunch of XMLs is used](https://stackoverflow.com/questions/36219677/visual-studio-2015-android-is-missing-mk-files-for-ndk-native-development)*. But why do you care? – Alex Cohn Jun 13 '17 at 09:23
  • 2
    As of 2018-15-01, the link to the Binglong article leads to a wall that states that the article in question is private, thus rendering it unaccessible to pretty much everyone who has not been given privileges by the article's author. – Adrien Jan 15 '18 at 12:59
  • 1
    @Adrien, thanks for letting know. At any rate, I did post the summary of that article. Note that it isn't relevant these days: current NDK r16 supports C++11 and even C++17, but gcc toolchain is going away in the next release. Also, eclipse has been deprecated for many years now. – Alex Cohn Jan 15 '18 at 17:39
2

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

Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
Aqua
  • 716
  • 8
  • 10
0

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.

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.

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:

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.