Standalone Toolchains

You can use the toolchains provided with the Android NDK independently or as plug-ins with an existing IDE. This flexibility can be useful if you already have your own build system, and only need the ability to invoke the cross-compiler in order to add support to Android for it.

Selecting Your Toolchain

Before anything else, you need to decide which processor architecture your standalone toolchain is going to target. This is done with the --arch flag.

Selecting Your Sysroot

The next thing you need to do is define your sysroot. A sysroot is a directory containing the system headers and libraries for your target. To define the sysroot, you must must know the Android API level you want to target for native support; available native APIs vary by Android API level.

Libraries for native APIs for the respective Android API levels reside under $NDK/platforms/; each API-level directory, in turn, contains subdirectories for the various CPUs and architectures. The headers reside in $NDK/sysroot.

For more detail about the Android API levels and the respective native APIs they support, see Native APIs.

Creating the Toolchain

The NDK provides the make_standalone_toolchain.py script to allow you to perform a customized toolchain installation from the command line.

This is a new tool that replaces the old make-standalone-toolchain.sh. It has been reimplented in Python so that Windows users will not need to install Cygwin or MSys to take advantage of this tool.

The script is located in the $NDK/build/tools/ directory, where $NDK is the installation root for the NDK.

An example of the use of this script appears below:

$NDK/build/tools/make_standalone_toolchain.py \
    --arch arm --api 21 --install-dir /tmp/my-android-toolchain

This command creates a directory named /tmp/my-android-toolchain/, containing a copy of the android-21/arch-arm sysroot, and of the toolchain binaries for a 32-bit ARM target.

Note that the toolchain binaries do not depend on or contain host-specific paths. In other words, you can install them in any location or even move them if you need to.

The --arch argument is required, but the STL will default to gnustl and the API level will be set to the minimum supported level for the given architecture (currently 14 for 32-bit architectures and 21 for 64-bit architectures) if not explicitly stated.

Every standalone toolchain is useable for both Clang and GCC. For more information about Clang, see Clang's website, especially the GCC compatibility section.

You may specify --stl=stlport to copy libstlport instead of the default libgnustl. If you do so, and you wish to link against the shared library, you must explicitly use -lstlport_shared. This requirement is similar to having to use -lgnustl_shared for GNU libstdc++.

Similarly, you can specify --stl=libc++ to copy the LLVM libc++ headers and libraries. Unlike gnustl and stlport, you do not need to explicitly pass -lc++_shared to use the shared library. The shared library will be used by default unless building a static executable. To force the use of the static library, pass -static-libstdc++ when linking. This behavior matches that of a normal host toolchain.

As mentioned in C++ Library Support, you will often need to pass -latomic when linking against libc++.

Note that if you omit the --install-dir option, the tool creates a tarball in the current directory named $TOOLCHAIN_NAME.tar.bz2. The tarball can be placed in a different directory by using --package-dir.

For more options and details, use --help.

Working with Clang

Clang binaries are automatically included in standalone toolchains.

This operation also installs two wrapper scripts, named clang and clang++, under <install-dir>/bin. These scripts invoke the clang binary with the correct target architecture flags. In other words, they should work without any modification, and you should be able to use them in your own builds by just setting the CC and CXX environment variables to point to them.

Clang targets with ARM

When building for ARM, Clang changes the target based on the presence of the -march=armv7-a and/or -mthumb compiler flags:

Table 1. Specifiable -march values and their resulting targets.

-march value Resulting target
-march=armv7-a armv7-none-linux-androideabi
-mthumb thumb-none-linux-androideabi
Both -march=armv7-a and -mthumb thumbv7-none-linux-androideabi

You may also override with your own -target if you wish.

clang and clang++ should be drop-in replacements for gcc and g++ in a makefile. When in doubt, use the following options when invoking the compiler to verify that they are working properly:

  • -v to dump commands associated with compiler driver issues
  • -### to dump command line options, including implicitly predefined ones.
  • -x c < /dev/null -dM -E to dump predefined preprocessor definitions
  • -save-temps to compare *.i or *.ii preprocessed files.

ABI Compatibility

By default, an ARM Clang standalone toolchain will target the armeabi-v7a ABI. GCC will target armeabi. Either can be controlled by passing the appropriate -march flag, and Clang can also be controlled with -target.

To target armeabi-v7a with GCC, you must set the following flags when invoking the compiler: -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16.

The first flag targets the armv7 architecture. The second flag enables hardware-FPU instructions while ensuring that the system passes floating-point parameters in core registers, which is critical for ABI compatibility.

We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions (Thumb-1 for armeabi). If omitted, the toolchain will emit 32-bit ARM instructions.

To use NEON instructions, you must use the -mfpu compiler flag: -mfpu=neon.

Note that this setting forces the use of VFPv3-D32, per the ARM specification.

Also, make sure to provide the following two flags to the linker: -march=armv7-a -Wl,--fix-cortex-a8.

The first flag instructs the linker to pick toolchain libraries which are tailored for armv7-a. The 2nd flag is required as a workaround for a CPU bug in some Cortex-A8 implementations.

You don't have to use any specific compiler flag when targeting the other ABIs.

To learn more about ABI support, see ABIs.

Warnings and Limitations

Windows support

The Windows binaries do not depend on Cygwin. This lack of dependency makes them faster. The cost, however, is that they do not understand Cygwin path specifications like cygdrive/c/foo/bar, as opposed to C:/foo/bar.

The NDK build system ensures that all paths passed to the compiler from Cygwin are automatically translated, and manages other complexities, as well. If you have a custom build system, you may need to resolve these complexities yourself.

Exceptions, RTTI, and STL

The toolchain binaries support C++ exceptions and RTTI by default. To disable C++ exceptions and RTTI when building sources (to generate lighter-weight machine code, for example), use -fno-exceptions and -fno-rtti.

C++ STL support

The standalone toolchain includes a copy of a C++ Standard Template Library implementation. This implementation is either for GNU libstdc++, STLport, or libc++, depending on what you specify for the --stl=<name> option described previously. To use this implementation of STL, you need to link your project with the proper library:

  • Use -static-libstdc++ when using libc++ for the static library version, or -lstdc++ (implicit) to link against the static library version of any other implementation. Doing so ensures that all required C++ STL code is included into your final binary. This method is ideal if you are only generating a single shared library or executable.

    This is the method that we recommend.

  • Alternatively, use -lgnustl_shared to link against the shared library version of GNU libstdc++. If you use this option, you must also make sure to package libgnustl_shared.so with your app in order for your code to load properly. Table 2 shows where this file is for each toolchain type.

  • Use -lstlport_shared to link against the shared library version of STLport. When you do so, you need to make sure that you also package libstlport_shared.so with your app in order for your code to load properly. Table 2 shows where this file is for each toolchain:

  • The shared library version of libc++ will be used by default. If you use this option, libc++_shared.so must be packaged with your app or your code will not load. Table 2 shows where this file is for each architecture.

    Table 2. Specifiable -march values and their resulting targets.

    Toolchain Location
    arm $TOOLCHAIN/arm-linux-androideabi/lib/
    arm64 $TOOLCHAIN/aarch64-linux-android/lib/
    x86 $TOOLCHAIN/i686-linux-android/lib/
    x86_64 $TOOLCHAIN/x86_64-linux-android/lib/

The reason the shared version of the libraries is not simply called libstdc++.so is that this name would conflict at runtime with the system's own minimal C++ runtime. For this reason, the build system enforces a new name for the GNU library. The static library does not have this problem.

Building Open Source Projects Using Standalone Toolchains

Given this example toolchain:

# Create an arm64 API 26 libc++ toolchain.
$NDK/build/tools/make_standalone_toolchain.py \
  --arch arm64 \
  --api 26 \
  --stl=libc++ \
  --install-dir=my-toolchain

Here's how you'd set up your environment to use it to build a traditional open source project:

# Add the standalone toolchain to the search path.
export PATH=$PATH:`pwd`/my-toolchain/bin

# Tell configure what tools to use.
target_host=aarch64-linux-android
export AR=$target_host-ar
export AS=$target_host-clang
export CC=$target_host-clang
export CXX=$target_host-clang++
export LD=$target_host-ld
export STRIP=$target_host-strip

# Tell configure what flags Android requires.
export CFLAGS="-fPIE -fPIC"
export LDFLAGS="-pie"

As an example, here's how to build toybox after performing the previous steps:

git clone https://github.com/landley/toybox.git
cd toybox
make defconfig && make

Alternatively a configure-based project would look more like this:

./configure && make