Does any android ndk compiler, based on gcc 4.7, support to_string, stof, and other string conversion functions?
In the NDK I am using I found the function in ext/vstring.h. But the function is wrapped in
#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99))
I am enabling -std=c++11 flag.
How do I enable GXX_EXPERIMENTAL_CXX0X?
I've enabled it by just adding it as a symbol in Project properties->C/C++ General->Symbols->GNU C++
I also added this in Android.mk:
LOCAL_CPPFLAGS := -std=gnu++0x
Related
I'm compiling Qt/C++ project with android NDK standalone toolchain. I’ve created standalone toolchain with make-standalone-toolchain.sh --arch=arm --toolchain=arm-linux-androideabi-4.9 --platform=android-21 command. NDK version is android-ndk-r10e. Target project uses some functions from pthread library. At compile time, I get the following error:
error: 'pthread_getaffinity_np' was not declared in this scope
const int err = pthread_getaffinity_np(_pthreadId, sizeof(cpu_set_t), &cpuSetMask);
compilation terminated due to -Wfatal-errors.
I've checked the header of pthread included in ndk toolchain and I did not find the declaration of pthread_getaffinity_np function.
Is pthread functionality for Android limited? How to use pthread with Android NDK properly?
Is pthread functionality for Android limited?
AFAIK, Yes.
http://mobilepearls.com/labs/native-android-api/#pthreads
https://web.archive.org/web/20180602101341/http://mobilepearls.com/labs/native-android-api/#pthreads
POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no
additional linking (-lpthreads) is necessary. It does not implement full
POSIX threads functionality and leaves out support for read/write locks,
pthread_cancel(), process-shared mutexes and condition variables as well as
other more advanced features. Read the bionic OVERVIEW.txt for more
information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available
to applications, lower than the posix minimum of 128.
See https://android.googlesource.com/platform/bionic/+/master/docs/status.md for our official docs about what is in which Android version.
you can also look at the <pthread.h> header in the NDK (current version here) and see for example entries like:
pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);
this shows that we do have the non-POSIX/non-portable (_np) function pthread_gettid_np, but that it was introduced in API level 21, so if your code needs to run on older releases you can't use it.
basically the headers are the canonical source of truth for "which functions are available in which API levels?".
for the specific case of pthread_getaffinity_np, no, we don't support that. you can combine pthread_gettid_np from <pthread.h> and sched_getaffinity from <sched.h> though.
POSIX threads (pthreads) seems to be not provided for -host build modules.
at least here is the error for the libcrypto-host module build:
out/host/linux-x86/obj/SHARED_LIBRARIES/libcrypto-host_intermediates/src/crypto/thread_pthread.o:
In function `thread_local_init':
/media/compilation/projects/android/beagle2/external/boringssl/src/crypto/thread_pthread.c:112:
undefined reference to `pthread_key_create'
and the only way to fix it so far is to add -lpthread inside
external/boringssl/Android.mk before directive:
include $(BUILD_HOST_SHARED_LIBRARY)
example:
# Host shared library
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libcrypto-host
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_MULTILIB := both
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk
LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -Wno-unused-parameter
LOCAL_CFLAGS += -DOPENSSL_NO_ASM
LOCAL_LDLIBS += -lpthread
include $(LOCAL_PATH)/crypto-sources.mk
include $(BUILD_HOST_SHARED_LIBRARY)
I am trying to use C++11 threading facilities with Android NDK, but not sure how to make it use the latest compilers.
I have Clang 3.2 and can build iOS apps. I wonder if there is a way to do it with Android NDK?
If not, then how should I build with gcc 4.8?
(I'm addressing the NDK version r9b)
To enable C++11 support for all source code of the application (and so any modules included) make the following change in the Application.mk:
# use this to select gcc instead of clang
NDK_TOOLCHAIN_VERSION := 4.8
# OR use this to select the latest clang version:
NDK_TOOLCHAIN_VERSION := clang
# then enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11
# or use APP_CPPFLAGS := -std=gnu++11
Otherwise, if you wish to have C++11 support only in your module, add this lines into your Android.mk instead of use APP_CPPFLAGS
LOCAL_CPPFLAGS += -std=c++11
Read more here:
http://adec.altervista.org/blog/ndk_c11_support/
NDK revision 10 has the Clang 3.6 toolchain. Use it:
NDK_TOOLCHAIN_VERSION := clang3.6
or use the latest available Clang toolchain
NDK_TOOLCHAIN_VERSION := clang
NDK revision 8e has the Clang 3.2 compiler bundled in it. Use it and you're good to go.
First, to decide which toolchain to use, edit your "application.mk" (do not confuse with android.mk) and insert for gcc 4.8:
NDK_TOOLCHAIN_VERSION := 4.8
or if you want clang:
NDK_TOOLCHAIN_VERSION := clang
But this has nothing to do with threads. This will only define which toolchain to use.
Now about threads, here is a simple example for android NDK:
#include <pthread.h> // <--- IMPORTANT
// This will be used to pass some data to the new thread, modify as required
struct thread_data_arguments
{
int value_a
bool value_b;
};
//---------------------------------
// This function will be executed in the new thread, do not forget to put * at the start of the function name declaration
void *functionRunningInSeparateThread(void *arguments)
{
struct thread_data_arguments *some_thread_arguments = (struct thread_data_arguments*)arguments;
if (some_thread_arguments->value_b == true)
{
printf("VALUE= %i", some_thread_arguments->value_a);
}
// Signal the end of the thread execution
pthread_exit(0);
}
//---------------------------------
// This is the actual function creating and starting the new thread
void startThread()
{
// Lets pass some data to the new thread, you can pass anything even large data,
// for that you only need to modify thread_data_arguments as required
struct thread_data_arguments *some_thread_arguments;
some_thread_arguments = (thread_data_arguments*)malloc(sizeof(*some_thread_arguments));
some_thread_arguments->value_a = 12345;
some_thread_arguments->value_b = true;
// Create and start the new thread
pthread_create(&native_thread, NULL, functionRunningInSeparateThread, (void*)some_thread_arguments)
}
For ndk builds, open Application.mk and add following info. in it (if using r8e):
NDK_TOOLCHAIN_VERSION=4.7
Note: Please use 4.8 in case you are using NDK revision 9.
Note that Android gcc support is now deprecated. You should now be using clang. Please read the version 11 release notes. You can specify:
NDK_TOOLCHAIN_VERSION=clang
To use the latest version based on your installed NDK. Also---as of this writing---the latest NDK (v12) is only accessable via Android Studio, and not through either the Downloads page or Standalone SDK Manager.
How do I integrate C++11 into Android?
It appears the main answer here includes experimental support for C++11, and C++11 is not experimental anymore.
If you're using command line NDK support (I use IDEA community edition 13 for the Java stuff), then this is what I had to put in my jni/Application.mk to get C++11 support with API 19 (on OSX ML):
NDK_TOOLCHAIN_VERSION := 4.8
# APP_STL := stlport_shared --> does not seem to contain C++11 features
APP_STL := gnustl_shared
# Enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11
Derived from here and here.
First of all, you will need to ensure that your toolchain is "Cross GCC". Whilst it was the default on my Linux, it was not on my MacOSX Lion.
In order to do this, go to Project Properties > C/C++ Build > Tool Chain Editor. "Current toolchain" should be set to "Cross GCC". You might need to uncheck the box "Display compatible toolchains only".
Then, add an option to LOCAL_CFLAGS in Android.mk:
LOCAL_CFLAGS := -std=gnu++11
We now need to inform Eclipse about where to find the corresponding new symbols (e.g. "std::unordered_map"). Go to Right Click on "jni" > Properties > C/C++ General -> Paths and Symbols -> Symbols -> GNU C++, and add the following symbol (by clicking "Add..."):
Name: __GXX_EXPERIMENTAL_CXX0X__
Value:
(i.e. let "Value" empty)
You can also set this in your build.gradle file if you are using the gradle-experimental-plugin:
android.ndk {
moduleName = "hello-android-jni"
stl = "stlport_shared"
cppFlags.add("-std=c++11")
}
With the latest gradle-experimental-plugin 0.8.0-alpha4 add to your app/build.gradle:
model {
android {
ndk {
moduleName "native"
CFlags.add("-std=c11") // Enable C11 support
ldLibs.add("log")
}
}
}
What I want to do (high-level): use qsort_r on Android.
There is no default implementation. So I've grabbed one from BSD. Unfortunately it needs fls functions which is also unavailable on Android. So I've grabbed Apple Open Source Libc library and copied ARM implementation into an inline assembly. Now I'm getting this:
Assembler messages:
Error: selected processor does not support Thumb mode `clz r3,r0'
Error: cannot honor width suffix -- `rsb r0,r3,#32'
AFAIR ARM-6 doesn't support it in Thumb mode. So how can I force non-Thumb mode for this one file, or is pure C implementation available for fls?
(and God, why do I have to play such low-level just to have qsort_r...)
In your Android.mk file, here is how to set things up to compile thumb, arm and neon versions of your code. The assembly language source files need to have the "S" capitalized in the makefile, but the actual name doesn't need to be capitalized. The suffixes ".arm" and ".arm.neon" are only in the makefile and not part of the name (e.g. the files below are named main.c, main_asm.s, test.c and test_asm.s).
LOCAL_ARM_MODE := arm # remove this if you want thumb mode
LOCAL_ARM_NEON := true # remove this if you want armv5 mode
# this flag will allow neon intrinsics in your C files
LOCAL_CFLAGS := -mfpu=neon -march=armv7
LOCAL_SRC_FILES := \
main.c.arm \
test.c.arm.neon \
main_asm.S.arm \
test_asm.S.arm.neon \
I am using open-ssl source given at https://github.com/eighthave/openssl-android
to build a library which can be used in android project.
As per instructions given at README.txt, I am able to compile it for the the Android platform version 2.2 (level -8)
But my app requires it to be 2.1 (level -7) compatible.
I tried following options with the default.properties file ( https://github.com/eighthave/openssl-android/blob/master/default.properties )
1) set target=android-7
2) set target=android-5
But when I compile it using command ndk-build, it gives following error
Compile thumb : crypto <= dsa_vrf.c
Compile thumb : crypto <= dso_dl.c
Compile thumb : crypto <= dso_dlfcn.c
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c: In function 'dlfcn_pathbyaddr':
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c:445: error: 'Dl_info' undeclared (first use in this function)
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c:445: error: (Each undeclared identifier is reported only once
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c:445: error: for each function it appears in.)
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c:445: error: expected ';' before 'dli'
/Crypto/openssl-android/crypto/dso/dso_dlfcn.c:455: error: 'dli' undeclared (first use in this function)
make: *** [obj/local/armeabi/objs/crypto/dso/dso_dlfcn.o] Error 1
As per error message- Dl_info is not defined. but if we go to file dso_dlfcn.c , the definition for the structure is already provided. (https://github.com/eighthave/openssl-android/blob/master/crypto/dso/dso_dlfcn.c)
And this code compiled for target=android-8 in default properties file, but not for android-7 or android-5.
Request you to help me to resolve this error. and let me know what all changes needs to be done in order to compile it for android 2.1 platform.
Thanks in advance.
Try to include the following piece of code into dso_dlfcn.c:
typedef struct {
const char *dli_fname; /* Pathname of shared object that
contains address */
void *dli_fbase; /* Address at which shared object
is loaded */
const char *dli_sname; /* Name of nearest symbol with address
lower than addr */
void *dli_saddr; /* Exact address of symbol named
in dli_sname */
} Dl_info;
int dladdr(const void *addr, Dl_info *info) { return 0; }
Before:
#ifdef __linux
# ifndef _GNU_SOURCE
# define _GNU_SOURCE /* make sure dladdr is declared */
# endif
#endif
After that in my case the library is built.
Try to install with latest NDK version and update Application.mk file appropriately.
LOCAL_PATH := $(call my-dir)
APP_PLATFORM := android-19
NDK_TOOLCHAIN_VERSION := clang
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -DANDROID
APP_PROJECT_PATH := $(shell pwd)
APP_BUILD_SCRIPT := $(LOCAL_PATH)/../Android.mk
The above 2 problems will be solved.
I had one issue with #Yuri's solution and had to improve it alittle. My APP_ABI is set to all in Application.mk. In my case it meant that among armeabi and armeabi-v7a I'm building also for x86 and mips. I have also android-9 target installed in android sdk to use in other projects. x86 and mips are supported by ndk starting from android-9. As written in the docs, when ndk-build will start building these targets, it will switch to android-9 target automatically. And what? - Yes, it will fail to compile :-). Here is my solution:
In crypto/Android.mk find the line local_c_flags :=
-DNO_WINDOWS_BRAINDEATH. After the line write this http://pastebin.com/7euUVD7A.
Yuri's code has to be inserted into if defined: http://pastebin.com/V58gTSBU
By the way, I've inserted Yuri's block after #include <openssl/dso.h> but not before #ifdef __linux