13

I can't figure out how to use shared pointers within my Android project. I'm using the latest Eclipse ADT on Mac OS X with the Android NDK r8d.

Here is what is in my Android.mk file:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CPPFLAGS  := -std=c++11
LOCAL_MODULE    := native
LOCAL_SRC_FILES := native.cpp
include $(BUILD_SHARED_LIBRARY)

Here is what is in my Application.mk file:

NDK_TOOLCHAIN_VERSION=4.7
APP_STL := stlport_shared

I've tried the default GCC 4.6, the experimental 4.7, and the clang3.1 toolchains.
I've tried linking to stlport_shared and gnustl_shared c++ runtime libraries.
I've tried the FLAGS -std=c++11, -std=c++0x, and the -std=gnu++11.

I'm able to use lambdas and auto of the c++11 standard, so the C++11 flag appears to be working. However anytime I try and use a shared_ptr, weak_ptr, or unique_ptr, I get the error 'suchandsuch_ptr' is not a member of 'std'

I have the #include <memory> in my cpp file. Now Eclipse tells me Unresolved inclusion: <memory>, but I get the same thing for <vector> and <string> and those appear to compile and work just fine.

Are smart pointers not implemented in the toolchains included in the Android NDK?
If not, why not? Since GCC and clang have had support for smart pointers for quite some time, this would mean that I am either missing something, or the Android devs have disabled them for some reason.
Any clues?

Mazyod
  • 22,319
  • 10
  • 92
  • 157
BigMacAttack
  • 4,479
  • 3
  • 30
  • 39
  • Are you sure it won't compile, or it's only the editor not resolving the inclusion? Anyway, the problem is certainly that it's pointing to the old header directory… I remember I had this but I'm not using CDT build any more. – Potatoswatter Jan 25 '13 at 23:49
  • Its not just the editor not resolving the inclusion. Its also not compiling. But it does compile for and references even though the editor can't resolve those either. For my project, the gnuc++ include directory is pointing to /android-ndk-r8d/platforms/android-14/arch-arm/usr/include – BigMacAttack Jan 26 '13 at 00:00
  • 1
    There should be another include directory which ends in `c++/`. Also, "Are smart pointers not implemented in the toolchains included in the Android NDK?" — as far as I know, GCC 4.7 is not included in the Android NDK. What specifically are you using? – Potatoswatter Jan 26 '13 at 00:04
  • 1
    According to http://developer.android.com/tools/sdk/ndk/index.html, they added the GCC 4.7 compiler to r8d: **"-Added the GNU Compiler Collection (GCC) 4.7 compiler to the NDK"**. When I add `/android-ndk-r8d/sources/cxx-stl/stlport/stlport` or `/android-ndk-r8d/sources/cxx-stl/gnu-libstdc++/4.7/include` I'm now able to compile! However, I'm still getting IDE errors that say various symbols can't be resolved. – BigMacAttack Jan 26 '13 at 00:25
  • 1
    You'll have to use the indexer settings to choose which toolchain/target settings it uses. – Potatoswatter Jan 26 '13 at 00:28
  • and that is why people like good ol' normal pointers... because smart pointers are lazy (you know, not working and all). – thang Jan 26 '13 at 00:31
  • @Potatoswatter Awesome. According to http://stackoverflow.com/a/9376753/1190255, I needed to add some additional paths to get rid of the `` and `` indexer errors. But the smart pointer indexer errors are still around. I might be missing another path still I suppose. – BigMacAttack Jan 26 '13 at 00:41
  • @thang Smart pointers aren't lazy, they're a smart way to manage owned pointers. Especially if you use exceptions in your code. – miguel.martin Jan 26 '13 at 01:40
  • @BigMacAttack Ah, now I remember, I had to add `__GXX_EXPERIMENTAL_CXX0X__` as a predefined macro to the indexer settings, to emulate `-std=c++11`. – Potatoswatter Jan 26 '13 at 03:08

2 Answers2

7

Be sure that the standard library include path (like /android-ndk-r8d/sources/cxx-stl/gnu-libstdc++/4.7/include) is in the target settings.

To get the IDE to recognize C++11 classes in the GNU standard library, add __GXX_EXPERIMENTAL_CXX0X__ as a predefined macro to the indexer. (The name is a bit of anachronism since C++11 is standardized and the support is no longer experimental, but as yet that's what it's called.) Also, be sure the indexer is set to reflect the correct build target.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Yup. This did it. I can now compile and there are no more indexer errors for c++11 classes. Thank you for your help! – BigMacAttack Jan 26 '13 at 03:19
  • I'm using Indigo for Android NDK development. I could not find a clear way to "add a predefined macro to the indexer". What is the exact procedure for setting the __GXX_EXPERIMENTAL_CXX0X__ macro? – Ariel Malka Mar 11 '13 at 10:52
  • 1
    @ArielMalka Preferences => C/C++ => Code Assistance => C++ Compiler => Macro Definitions => Add. – Potatoswatter Mar 12 '13 at 00:44
  • 1
    This didn't fix the Eclipse indexer for me (using Eclipse Juno), so I posted a hack at "http://stackoverflow.com/questions/9375708/eclipse-indexer-errors-when-using-stl-with-android-ndk/15782177#15782177" that worked for me. – Shervin Emami Apr 03 '13 at 08:22
  • @BigMacAttack how to add library path? – Utkarsh Srivastav Nov 26 '14 at 15:10
4

these settings did it for me:

Application.mk:

NDK_TOOLCHAIN_VERSION=4.7
APP_STL := gnustl_static

Android.mk:

LOCAL_CFLAGS :=-D__GXX_EXPERIMENTAL_CXX0X__  <--important
LOCAL_CPPFLAGS  := -std=c++11

Eclipse Settings: C/C++ General\Path and Symbols should contain:

C:\android\ndk\sources\cxx-stl\gnu-libstdc++\4.7\include
C:\android\ndk\sources\cxx-stl\gnu-libstdc++\4.7\libs\armeabi\include
pulp
  • 698
  • 1
  • 6
  • 13
  • that was the "-D__GXX_EXPERIMENTAL_CXX0X__" ! I thought it would be implied by the -std=c++11, but it works this way ! Thanks – johan d Apr 04 '14 at 23:45
  • I have done in the similar manner stated above but i am getting the error java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol `"_ZNSt13_Filebuf_base12_M_page_sizeE"`.Do you have any idea about the same – Aagman Nov 10 '16 at 07:51