I'm trying to use chrono with the Android NDK. I had some success so far but some features are not supported.
I added this line in my Android.mk:
LOCAL_CPPFLAGS := -std=c++11
My Application.mk file:
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_PLATFORM := android-9
The first problem is that steady_clock is not defined. This line:
std::chrono::steady_clock::time_point time = std::chrono::steady_clock::now();
generates this compile error:
error: 'std::chrono::steady_clock' has not been declared
I found out I can use monotonic_clock instead and it works, but this type is supposed to have been replaced by steady_clock.
The second problem is that the property is_steady is not defined for any type:
LOGD("high_resolution_clock is steady: ", std::chrono::monotonic_clock::is_steady);
generates this compile error:
error: 'is_steady' is not a member of 'std::chrono::monotonic_clock'
Does someone know if there is full support for chrono in the NDK? I also would like to know if it's a good idea to use c++11 with the NDK. I'm not sure if it's stable or it's likely to change in the future.
Well I just found a solution. Looks like you need to tell the gcc version you are going to use (I guess it's the 4.7 by default). I just added this line in my Application.mk
NDK_TOOLCHAIN_VERSION := 4.8
Now I can use steady_clock and is_steady.
Related
I'm having problems with the android ndk r10. I get the following: std::string has not been declared. I watched other forum threads like this, but nope of them helped me. My Application.mk looks like this:
APP_PLATFORM := android-14
APP_ABI := armeabi-v7a # build for the ARM version of MCPE
APP_CFLAGS := -O2 -std=gnu99 # optimization level 2, use C99 (for decleations in for loops, etc)
APP_CPPFLAGS := -std=c++11
LOCAL_CFLAGS := -std=c++11
APP_STL := stlport_static
Please help me!
The class std::string has not been declared because it's not there. Unfortunately, there is no std::string in NDK. Some ports of std::string exist, I remember I found one or two, but finally I decided just not to use std::string. Why? Because we already have Java strings and C strings, Java strings come from JNI calls, C strings come from files, and if you introduce one more string type, it will be more conversion than usage.
EDIT: but read the comment below.
Am trying to build an android ndk app using clang instead of gcc, for know i have tried this
in the Android.mk
NDK_TOOLCHAIN_VERSION := clang
LOCAL_CLANG :=true
LOCAL_LDLIBS := -lc++_static
LOCAL_CFLAGS := -std=c++11
and in the Application.mk
APP_PLATFORM := android-9
APP_STL := libc++_static
APP_CPPFLAGS := -fexceptions -frtti
APP_ABI := armeabi-v7a
but it always give me link errors with the std library.
Any help is appreciated !
There are several mistakes in your *.mk files:
libc++_static isn't a proper value for APP_STL, it should be c++_static here.
NDK_TOOLCHAIN_VERSION has no effect when set inside Android.mk, it should be set inside Application.mk
LOCAL_CLANG is a variable used inside system modules from AOSP, not when using the NDK.
Since you're setting APP_STL as c++_static, the NDK toolchain will correctly tell the linker what lib to use, you shouldn't add LOCAL_LDLIBS := -lc++_static.
Also, you set APP_ABI to only armeabi-v7a, is it on purpose ? Android runs on other architectures as well and you'll get better performance on these if you also compile your libraries accordingly. You can either set APP_ABI to all or to a list of architectures armeabi-v7a x86...
In summary:
Android.mk
LOCAL_CFLAGS := -std=c++11
Application.mk
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-9
APP_STL := c++_static
APP_CPPFLAGS := -fexceptions -frtti
APP_ABI := all
If you continue having some troubles compiling your code, please show the exact errors you're getting.
The building settings are correct,
mostly this is happens because you are linking with library that use gcc instead of clang. check if all your linked library using clang !
I know there is a possible dup here:
error: 'stod' was not declared in this scope but I already tried what advised there.
I try to compile a C++ application in Android NDK.
I get error:
'stod' was not declared in this scope
I enabled C++ 11 in Application.mk:
APP_ABI := x86
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION:= 4.8
APP_CPPFLAGS += -fexceptions -std=c++11 -fpermissive -frtti -pthread
What else should I do in order to get rid of this error?
I guess you probably figured out that this isn't part of the current Android NDK (10). There are several missing pieces from the C++11 libraries, such as regex functionality and to_string, etc.
I'm trying to compile a C++ code with ndk-build and I get the next error:
'sqrtl' was not declared in this scope
I include cmath in the source code and configure the application.mk as follow:
APP_CPPFLAGS := -frtti -fexceptions
APP_STL := gnustl_static
APP_ABI := armeabi-v7a
APP_PLATFORM := android-10
Searching into ndk sources, I found that sqrtl is defined in cmath, together with other xxxl functions (cosl, sinl, etc).
I don't know if missing a define or something like that.
Can someone help me ?
So you could find the functions in math.h, but according to
https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/0jVfs3wCeGs
android doesn't support long double so not sure you're going to get any success using the standard headers... Could you use the plain double version?
I try to use gnu++0x features in my native android code. I'm using the lastest version (r8b, with toolchain 4.6) and while code using std::chrono or std::thread works well using linux gcc 4.6, it seems that these features are not supported in android ndk.
To compile, I use the following flags in the Application.mk file :
APP_CPPFLAGS := -std=gnu++0x
APP_STL := gnustl_shared
APP_GNUSTL_CPP_FEATURES := rtti exceptions
May I do something wrong ? Is there a link to know the status of the c++0x support in android ndk ?
Thanks a lot !
Regards,
JB
Just a sample code like this :
std::chrono::time_point<std::chrono::system_clock> start =
std::chrono::system_clock::now();
std::chrono::milliseconds duration (100);
std::this_thread::sleep_for(duration);
std::chrono::milliseconds elapsedTime =
std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::system_clock::now() - start);
With headers,
#include <chrono>
#include <thread>
Using ndk-build, I've got an error message like this :
error: 'chrono' in namespace 'std' does not name a type
....
error: 'std::this_thread' has not been declared
And all about chrono and thread !
I know this is an old question, but I succeed in using chrono in the Android NDK. I just added this line to my Android.mk:
LOCAL_CPPFLAGS := -std=c++11
I also added this line in Application.mk to solve some issues I had using chrono:
NDK_TOOLCHAIN_VERSION := 4.8
Now you have support for c++11 including chrono.
<NDK_root_folder>/docs/CPLUSPLUS-SUPPORT.html
that's all, here you get all the informations and the support that your NDK can give to your C++ code.
have a try:
NDK_TOOLCHAIN_VERSION := 4.8
NDK_TOOLCHAIN_VERSION
Define this variable to either 4.4.3 or 4.6 to select version of GCC compiler.
4.6 is the default