Now we can use the tool chain provided by Android NDK in the form of an independent compiler! This is useful if you already have your own build system. A typical application scenario is to call a 'configure 'script dependent on the CC environment variable open source library for cross-platform compilation. This document explains how to do that. 1 select your tool chain: First, you need to determine your independent tool chain target CPU architecture, is ARM-based equipment, x86-based equipment, or MIPS-based equipment. Each architecture corresponds to a different tool chain Name: * arm-linux-androideabi-4.6 => for ARM-based Android devices * x86-4.6 => for x86-based Android devices * mipsel-linux-android-4.6 => for MIPS-based Android devices 2 choose your sysroot: the second thing you should know is that you want to target the Android nativeAPI. Each level provides different APIs that are described by the doc/STABLE-APIS.html and correspond to the subfolders of $ NDK/platforms. This allows you to define the path pointing to your 'sysroot'. The GCC path contains the system header file and library. It usually looks like this: SYSROOT = $ NDK/platforms/android-<level>/arch-<arch>/<level> is the number of API levels, <arch> is the architecture ("arm", "x86", and "mips" can both be used as values ). For example, if you are targeting Android2.2 (a.k. a. froyo), you should use: SYSROOT = $ NDK/platforms/android-8/arch-arm Note: X86 and MIPS systems are only supported in android-9. 3. Call the compiler (stupid way): Use the -- sysroot option to call the compiler to indicate the path of the system file for your platform. Example: export CC = "$ NDK/toolchains/<name>/prebuilt/<system>/bin/<prefix> gcc -- sysroot = $ SYSROOT" $ CC-o foo. o-c foo. c <name> is the name of the toolchain, <system> is the host system, and <prefix> is the prefix of a specific toolchain. For example, if you use the NDK r5 tool chain on Linux, you will use: exportCC = "$ NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -- sysroot = $ SYSROOT" as you can see, this is cool, but it does work! Note: using the NDK tool chain directly has many restrictions: you cannot use any C ++ STL (either STLport or GNU libstdc ++ ). You cannot use exceptions or RTTI. 4. Call the compiler (easy way): NDK allows you to create a "Custom" toolchain and install it to make life easier. For example, the following command: $ NDK/build/tools/make-standalone-toolchain.sh -- platform = android-5 -- install-dir =/tmp/my-android-toolchain which will create a folder, the name is/tmp/my-android-toolchain, which contains an android-5/arch-armsysroot test bay and an execution file of the tool chain. Note: by default, the ARM-based GCC 4.6 toolchain is selected by the script. Use '-- arch = x86' to specify the gcc of the x86-based, use '-- arch = mips' to specify the MIPS-based GCC, or use '-- toolchain = <name>' to specify. Example: -- toolchain = x86-4.4.3 # select x86 GCC 4.4.3 compiler -- toolchain = mipsel-linux-android-4.6 # select mips gcc 4.6 compiler, same as -- arch = mips if you want, you can add -- llvm-version = 3.1 to pass clang/llvm3.1. You can directly use it later. Like: exportPATH =/tmp/my-android-toolchain/bin: $ PATH exportCC = arm-linux-androideabi-gcc # or export CC = clang exportCXX = arm-linux-androideabi-g ++ # or export CXX = clang ++ note, without the -- install-dir option, the make-standalone-toolchain.sh creates a tarball named/tmp/ndk/<toolchain-name> .tar.bz2. This allows you to easily store and re-release the binary toolkit. Another important benefit is that this independent tool chain will include a GNU libstdc ++ kibbe that supports exceptions and RTTI (when you link to libstdc ++ or libsupc ++ ). Key: toolchain execution files do not depend on or contain characteristic paths on the host. In other words, they can be installed anywhere or moved to another location. Note: you can still use the-sysroot option for the new tool chain, but it is now simpler! 5 ABI compatibility: The machine code generated through the ARM tool chain should be compatible with the official Android 'armeabi 'ABI. We recommend that you use the-mthumb compilation flag to forcibly generate the 16-bit Thumb-1 command (the default value is 32-bit ). If you want to face 'armeabi-v7a 'ABI, you should make sure the following flag is used: CFLAGS ='-march = armv7-a-mfloat-abi = softfp-mfpu = vfpv3-d16 'Note: the First Flag enables the Thumb-2 command, and the second enables the H/w fpu command to ensure that the floating point parameter is passed into the core register, which is the key to ABI compatibility. * Do not * use these labels separately! If you want to use the Neon command, you need to change the-mfpu compilation flag: CFLAGS = '-march = armv7-a-mfloat-abi = softfp-mfpu = neon' note that this will force the use of VFPv3-D32. Make sure that the following two flags are also provided to the linker: LDFLAGS = '-march = armv7-a-Wl, -- fix-cortex-a8' Note: The first sign instructs the linker to choose the appropriate libgcc for the armv7-a. a. libgcov. a and crt *. o. The second sign is used to bypass a CPU bug in some Cortex-A8 devices. If the above does not satisfy you, you 'd better not use an independent tool chain, but stick to the NDK to build the system, which will handle all the details for you. When you are targeting x86 ABI or mips abi, you do not need to use any specific compiler flag. 6. Warning and restrictions: Windows support: the binary file in Windows does not depend on Cygwin. The good news is that they run fast. The bad news is that they do not understand the path form of Cygwin, such as/cygdrive/c/foo/bar (instead of C:/foo/bar ). The NDK build system ensures that all paths passed from Cygwin to the compiler are automatically converted, and you can cope with the difficult tasks. If you have a custom build system, you need to handle all the problems on your own. Note: There is no plan to support Cygwin/MSys. Wchar_t support: Before Android 2.3, the Android platform did not really support wchar_t. This indicates: l if you are targeting android-9 or a higher platform, the size of wchar_t is 4 bytes, in addition, wide-byte functions are available in most C libraries (except for multibyte codec functions and wsprintf/wsscanf functions ). L if you are targeting any lower API level, wchar_t has only one byte and cannot be used by any wide byte function. We recommend that all developers remove any dependency on wchar_t and convert it to a better way. Android only supports integrating existing code. Exception, RTTI, and STL: The plug-in execution file supports C ++ exceptions and RTTI. So when you don't need them, use-fno-exceptions and-fno-rtti to disable them (produce fewer machine codes ). Note: If you want to use these features, you need to explicitly link to libsupc ++. To do this, use-lsupc ++: arm-linux-androideabi-g ++ when linking to a binary file .... -lsupc ++ C ++ STL support: the independent tool chain also comes with a test bay of the GNU libstdc ++ library, which provides an implementation of the C ++ standard template library. To use it, you also need to link to the correct Library: l use-lstdc ++ to link to the static library version. This ensures that all the required C ++ STL code is included in your final file. This is recommended. L use-lgnustl_shared to link the dynamic library version. If you use this method, make sure that libgnustl_shared.so is also copied to your device. This file is located at $ TOOLCHAIN/arm-linux-androideabi/lib/for ARM toolchains. $ TOOLCHAIN/i686-linux-android/lib/for x86 ones. $ TOOLCHAIN/mipsel-linux-android/lib/for MIPS toolchains. the dynamic library version of GNU libstdc ++ is not called libstdc ++. so, this will lead to the minimum C ++ Runtime (/system/lib/libstdc ++. so) conflict. This does not affect static databases.