NDK compilation error: 'stod' was not declared in this scope - android

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.

Related

C++11 random library in Android JNI

I am trying to compile an Android app with a native component that uses the C++ random library.
My Application.mk file is:
APP_STL := stlport_static
APP_CPPFLAGS += -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
On compilation I get the error:
[armeabi] Compile++ thumb: Project <= main.cpp
/home/user/project/main.cpp:12:18: fatal error: random: No such file or directory
#include <random>
Is the random library available for Android?
APP_STL := stlport_static
APP_CPPFLAGS += -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
You're building agains STLPort - which as far as I can tell - is a C++03 standard library implementation. Hence it won't have C++11 headers.
Of the other options, you might consider:
APP_STL:= gnustl_static
or
APP_STL:= c++_static
Which give you GNU libstd++ or LLVM's libc++ respectively. If you are concerned about GPL polution to your app (as Google clearly are not using libstd++ by default), use libc++. You might as well you clang as the compiler as well at this point.

Using thread and mutex in Android Studio with ndk 10c

I have the latest version of the 64bit NDK (r10c), and the latest version of Android Studio I can download (0.8.14).
I am making a number of JNI calls to use String, Vector, Atomic, etc. But I can not figure out how to use thread and mutex.
Both of them give me the same error
Error:(92, 5) error: 'thread' is not a member of 'std'
Error:(93, 5) error: 'mutex' is not a member of 'std'
I'm sure that the NDK is using 4.9 of the gnu-libstdc++. If I put in a #error in the file I see my error and compilation stops. It appears that I'm not missing any defines since I can put the #error inside the class and see it.
Here is the ndk config in my build.gradle
ndk {
moduleName "myLib"
ldLibs "log"
stl "gnustl_shared"
cFlags "-std=c++11 -frtti -fexceptions -pthread"
}
The -frtti and -pthread seem to make no difference. I have also tried stl of gnustl_shared as well as gnustl_static, no difference.
By default, NDK still uses GCC 4.6 which has crippled support for C++11. You need the grade equivalent for setting NDK_TOOLCHAIN_VERSION:=4.9 in Application.mk. You can find some answers here: how to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build, but unfortunately the bottom line is that today you have to disable automatic ndk-build call by setting jni.srcDirs to empty and use the Android.mk and Application.mk files the old way.
So, if in your jni directory, there are files file1.cpp and file2.cpp, you will need the following Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myLib
LOCAL_SRC_FILES := file1.cpp file2.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -std=c++11 -frtti -fexceptions -pthread
... and Application.mk
APP_ABI := armeabi-v7a
APP_STL := gnustl_shared
NDK_TOOLCHAIN_VERSION := 4.9

build android with clang instead of gcc ? and the clang stl lib instead of gnustl lib?

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 !

chrono partially not supported in Android NDK

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.

'sqrtl' was not declared in this scope

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?

Categories

Resources