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.
Related
Do not think I did not search, my Android project (on Eclipse) refuses to recognize std::chrono library. The include is OK in my header file :
#include <chrono>
But when I want use it :
using namespace std::chrono;
I have a : Symbol 'chrono' could not be resolved, and all the functions of chrono are unavailables.
So I use NDK r10e, I add some lines in my Application.mk, which now looks like this :
APP_PLATFORM := android-22
APP_STL := gnustl_static
APP_CPPFLAGS := -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
And in my Android.mk, I add :
LOCAL_CFLAGS += -std=gnu++11
It did not solve my problem. Any ideas ? Bad Eclipse configuration ?
After modifications in mk files, I have build and re-build my project.
This is known problem of GNU libstdc++ in Android NDK. It's built on top of very limited libc (Google's Bionic) and thus can't provide full C++ Standard Library functionality. In particular, std::chrono is almost completely disabled at build time, but not only std::chrono. There are many other classes and functions being disabled, so Google's NDK just don't support C++ fully.
You can switch to LLVM libc++ (APP_STL := c++_static), but it has experimental status in Google's Android NDK and is actually unstable (i.e. it cause crashes in the application even for completely standard C++ code). This instability is caused by the same reason as for GNU libstdc++ - i.e. because it's built on top of very limited libc.
I'd recommend switch to CrystaX NDK - alternative fork of Google's Android NDK, which I've started mainly to solve Google's NDK problems such as non-standard implementations of libc, libc++, etc. CrystaX NDK is developed to work as a drop-in replacement for Google's NDK (except for the fact that it provides fully standard-conforming low-level libraries). In CrystaX NDK, both GNU libstdc++ and LLVM libc++ are much more stable and fully conforming to C++ standard, at least at the same level as they conform to it on GNU/Linux. In particular, std::chrono is fully implemented there and works just fine. Also, in CrystaX NDK, you can use more recent compilers such as gcc-5.3 and clang-3.7, with better support of C++11 and C++14. I'd be happy if it helps you.
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)
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")
}
}
}
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
I've downloaded a custom toolchain (linaro) to build ARM based Android apps. How do I tell the NDK to use it? Can I define or set something in Android.mk and Application.mk that would allow me to do that? Is there another way?
The NDK makefile system is quite extensible and you can indeed define a different toolchain. You'll need some understanding of how Make works.
Toolchains are discovered and initialized in build/core/init.mk line 261 (in NDKr6, the line # may change in future versions). The initialization code looks for files named config.mk under $(NDK_ROOT)/toolchains/*. So you'll need to add your toolchain in a subdirectory under the NDK toolchains directory, and add a config.mk and setup.mk to that subdirectory. Look at toolchains/x86-4.4.3 and toolchains/arm-linux-androideabi-4.4.3 for examples. You should be able to cut and paste the ARM toolchain config.mk and setup.mk if your toolchain has a standard layout.
Once you've defined a toolchain in the toolchain directory, you can switch to it by setting the NDK_TOOLCHAIN variable inside your Application.mk file.
As the other answer mentions, toolchains are discovered by ndk-build makefile system in $(NDK_ROOT)/toolchains/ and you can mirror ideas you see there. But there are a few extra concepts for supporting non-Android target platforms that are interesting although they may be soon outdated as ndk-build starts to explicitly support other platforms, such as mingw targeting win32 (or other gcc compilers targeting plain 'ol linux).
In config.mk:
TOOLCHAIN_ABIS := (list of ABIs that the toolchain supports)
This is an important definition, because you can use this name in your Application.mk to build using the toolchain for a particular ABI. One of the benefits of corrupting the usage of this definition, is that ndk-build can simultaneously build for multiple ABIs. It always assumes that the platform is Android, but if you want to target win32 using a mingw based toolchain, you can define an "ABI" as x86-win32, and then use this ABI in your Application.mk to select it as an additional target via APP_ABI:= x86-win32 Then in your Android.mk files you can use the TARGET_ARCH_ABI definition to select win32 specific sources and include paths, for example:
ifeq ($(TARGET_ARCH_ABI),x86-win32)
LOCAL_SRC_FILES += my_win32_file.c
LOCAL_CFLAGS += -DSOME_WIN32_SPECIFIC
endif
The final piece is that in setup.mk for your toolchain, it may be insufficient to look at other toolchains as examples, because what setup.mk for a particular toolchain really does is override build settings in default-build-commands.mk, so what you want to do is inspect that file, and redefine things in it that you don't like.
Following the previous example, mingw does not support the noexec flag in the binaries, and you can get rid of this feature by adding the following lines in your setup.mk:
# These flags are used to enforce the NX (no execute) security feature in the
# generated machine code. This adds a special section to the generated shared
# libraries that instruct the Linux kernel to disable code execution from
# the stack and the heap.
TARGET_NO_EXECUTE_CFLAGS := # our platform doesn't support this flag!
TARGET_NO_EXECUTE_LDFLAGS := # our platform doesn't support this flag!
# These flags disable the above security feature
TARGET_DISABLE_NO_EXECUTE_CFLAGS := # our platform doesn't support this flag!
TARGET_DISABLE_NO_EXECUTE_LDFLAGS := # our platform doesn't support this flag!
This is just one example of the many features in default-build-commands.mk that may need to be overridden, and of course it's important to provide TOOLCHAIN_NAME so that the toolchain can be selected via NDK_TOOLCHAIN variable inside your Application.mk file in addition to the ABI methodology I mention above.
well,you can simply add "NDK_TOOLCHAIN_VERSION = 4.9" in your Application.mk