5

I am compiling C++ library code in Android Studio 2.2. I follow the new guide, where I add all the files to my project and compile it using CMake (and CMakeLists.txt) like this. I want to use C++14 features, and stuff like atomic, stoi etc. but the building is failing with errors.

error: no type named 'condition_variable' in namespace 'std'
error: no member named 'stoi' in namespace 'std'

This is what my CMakeLists looks like (other lines set source files and other stuff):

find_library(GLES GLESv2)
include_directories(${COMMON_PATH} /usr/local/include)
set(ANDROID_STL "c++_shared")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -latomic")

add_library(native-lib SHARED ${COMMON_SRC})
target_link_libraries(native-lib ${GLES})

I found this article on the android page (here), but I don't know how and if I can do this when using CMakeLists and not ndk-build. I see other question that solve it using the c++_static runtime but only with ndk-build.

davidv
  • 373
  • 5
  • 14
  • 1
    Have you tried setting it from your gradle script, as described [here](https://developer.android.com/ndk/guides/cmake.html) ? – Michael Feb 06 '17 at 13:20
  • Did you try to set `CMAKE_CXX_STANDARD` as described [here](http://stackoverflow.com/questions/38132793/how-to-control-compiler-flag-invoked-when-specifing-cmake-cxx-standard)? – Florian Feb 06 '17 at 21:18

2 Answers2

6
  • This documentation would be more useful for cmake case: arguments "-DANDROID_STL=c++_shared
  • Gradle also packs libc++_shared.so into APK when using c++_shared; bypassing Gradle might cause trouble for your app on early version of Android OS.

at the time of this question, Android Studio might have trouble; at the time now Android Studio 3.1.3, it should be ok

Gerry
  • 1,223
  • 9
  • 17
0

The cross-compilation process used to generate the native libraries for Android uses the c++ dependencies from the NDK libraries. The NDK provided by Google is good and it has lots of things, but the C++11 and C++14 support is not complete.

If you want to use C++14 features, you can use other NDK like CrystaX NDK for example. With CrystaX you have also C++17 support.

goe
  • 2,272
  • 1
  • 19
  • 34