- Standalone toolchains (obsolete)
- Select your toolchain
- Select your sysroot
- Create the toolchain
- Work with Clang
- Clang targets with ARM
- ABI compatibility
- Warnings and limitations
- Windows support
- Exceptions, RTTI, and STL
- C++ STL support
- Build open source projects with standalone toolchains
- Projects with custom build systems
- Projects with autoconf
- Tydus / howto-standalone-toolchain.md
- This comment has been minimized.
- nikhilkilivayil commented Feb 5, 2016
- This comment has been minimized.
- MichaelSX commented Apr 13, 2016
- This comment has been minimized.
- usman313 commented May 1, 2016 •
- This comment has been minimized.
- ranshalit commented Feb 14, 2017
- This comment has been minimized.
- hannesa2 commented Dec 28, 2017 •
- This comment has been minimized.
- AxelNennker commented Apr 20, 2018
- This comment has been minimized.
- KrishnaCAmjuri commented Jun 2, 2018 •
- This comment has been minimized.
- unalkalkan commented Jun 3, 2018
- This comment has been minimized.
- KrishnaCAmjuri commented Jun 4, 2018
- This comment has been minimized.
- bybilly commented Aug 14, 2018
- This comment has been minimized.
- tonyw5 commented Aug 14, 2018
- This comment has been minimized.
- jainabhinav249 commented Mar 5, 2019
- This comment has been minimized.
- jainabhinav249 commented Mar 5, 2019
- This comment has been minimized.
- w1252675615 commented Sep 29, 2019
Standalone toolchains (obsolete)
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.
Select 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.
Select 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.
Create 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 run it.
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:
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 API level will default to the minimum supported level for the given architecture (currently 16 for 32-bit architectures and 21 for 64-bit architectures).
Since r18, all standalone toolchains use Clang and libc++. The libc++ 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 .
Work with Clang
Clang binaries are automatically included in standalone toolchains.
There are also two wrapper scripts, named clang and clang++ , under /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.
There are also wrapper scripts named gcc and g++ that also call Clang. This is to provide some level of compatibility for build files that explicitly refer to GCC even though the NDK no longer contains GCC. Obviously, if a build file uses command-line options that aren’t supported by Clang, you’ll need to remove or replace 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 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. This can be overridden by passing the appropriate -march or -target option.
We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions. 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 Android 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 .
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 C++ Standard Template Library (STL) implementation.
Use -static-libstdc++ to get the static library version of libc++. 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, which is our recommendation.
The shared library version of libc++ will be used by default. No additional flags are needed to link against the shared library. You must package libc++_shared.so in 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/ |
Note: If your project contains multiple shared libraries or executables, you must link against a shared-library STL implementation. Otherwise global state in these libraries will not be unique, which can result in unpredictable runtime behavior. This behavior may include crashes and failure to properly catch exceptions.
Build open source projects with standalone toolchains
Given this example toolchain:
Here’s how you’d set up your environment to use it to build a traditional open source project:
Projects with custom build systems
As an example, here’s how to build toybox after performing the previous steps:
Projects with autoconf
Alternatively a autoconf-based project would look more like this:
Note that autoconf-based projects vary wildly in their support for cross-compilation. Note also that if you git clone a autoconf-based project, it’s unlikely to have a checked-in configure script, so you’ll have to follow that project’s documentation for how to bootstrap.
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Tydus / howto-standalone-toolchain.md
HOWTO Cross compiling on Android
NDK (Native Develop Toolkit) is a toolchain from Android official, originally for users who writes native C/C++ code as JNI library. It’s not designed for compiling standalone programs (./a.out) and not compatible with automake/cmake etc.
What is Standalone Toolchain
«Standalone» refers to two meanings:
- The program is standalone (has nothing connect to NDK, and don’t need helper scripts to run it)
- The toolchain is made for building standalone programs and libs, and which can used by automake etc.
(Optional) Why NDK is hard to use
By default, NDK uses android flavor directory structure when it’s finding headers and libs, which is different from GNU flavor, so the compiler cannot find them. For Example:
Although we can manuall specify the path (someone wrote a program called «agcc» to handle this automatically, but still not good), it’s really annoying.
- Download Android NDK
from https://developer.android.com/tools/sdk/ndk/index.html - Extract the NDK
tar xf android-ndk-r9d-*.tar.bz2 && cd android-ndk-r9d - Make GNU Android Toolchain from NDK
build/tools/make-standalone-toolchain.sh —toolchain=arm-linux-androideabi-4.8 —platform=android-19 —install-dir=../toolchain - Delete the NDK (Yes, we don’t need it any more)
cd .. && rm -rf android-ndk-r9d - Test the native toolchain
cd toolchain/bin
echo «main()<>» | ./arm-linux-androideabi-gcc -x c —
file a.out # a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped - (Optional) Now you can use it as a standard GNU toolchain
For example: ./configure —prefix=/opt/android —host=arm-linux-androideabi && make && make install
This comment has been minimized.
Copy link Quote reply
nikhilkilivayil commented Feb 5, 2016
I am getting this issue :
/home/umn/android/android-ndk-r7c/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lssl
collect2: ld returned 1 exit status
This comment has been minimized.
Copy link Quote reply
MichaelSX commented Apr 13, 2016
Check if you have libssl-dev installed when using ubuntu reffering to this thread (even though it isn’t about android, but still about your issue).
This comment has been minimized.
Copy link Quote reply
usman313 commented May 1, 2016 •
I am getting this error each time I try to do any work on Android Studio 2.1 and everything in each activity become red. How can I resolve this issue??
This comment has been minimized.
Copy link Quote reply
ranshalit commented Feb 14, 2017
That’s amazing. Thank you very much. I don’t have to build now with «-static». Right ?
This comment has been minimized.
Copy link Quote reply
hannesa2 commented Dec 28, 2017 •
I downloaded android-ndk-r16b and after make-standalone-toolchain.sh the command
in step .5 echo «main()<>» | ./arm-linux-androideabi-gcc -x c — shows nothing.
Is this right, or did I something wrong ?
This comment has been minimized.
Copy link Quote reply
AxelNennker commented Apr 20, 2018
@hannesa2
echo «main()<>» | ./arm-linux-androideabi-gcc -x c — && file a.out
This comment has been minimized.
Copy link Quote reply
KrishnaCAmjuri commented Jun 2, 2018 •
Is this procedure valid for mac(unix) also?
This comment has been minimized.
Copy link Quote reply
unalkalkan commented Jun 3, 2018
Tried in Debian 9, works like a charm!
This comment has been minimized.
Copy link Quote reply
KrishnaCAmjuri commented Jun 4, 2018
This will only build the toolchain for one architecture that is «armv7-a». What about the other architectures? Can you please clarify?
This comment has been minimized.
Copy link Quote reply
bybilly commented Aug 14, 2018
I am having this issue:
»
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can’t open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can’t open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can’t open path: No such file or directory
./make-standalone-toolchain.sh: can’t open path: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_default_.=: not found
expr: syntax error
expr: syntax error
./make-standalone-toolchain.sh: can’t open name: No such file or directory
./make-standalone-toolchain.sh: OPTIONS_abstract_Specify: not found
./make-standalone-toolchain.sh: OPTIONS_default_linux-armv7l=: not found
[and there’s more]
«
At the top it says «WARNING: The shell running this script isn’t bash. Although we try to avoid bashism in scripts, things can happen.»
At the bottom it says that «—toolchain» is an unknown option. Even if —help is the first parameter it says it is an unknown option.
I’d appreciate any help. What can I do?
This comment has been minimized.
Copy link Quote reply
tonyw5 commented Aug 14, 2018
tonywilliams$ /Users/tonywilliams/android-ndk-r17b/build/tools/make-standalone-toolchain.sh —toolchain=arm-linux-androideabi-4.8 —platform=android-19 —install-dir=../toolchain
HOST_OS=darwin
HOST_EXE=
HOST_ARCH=x86_64
HOST_TAG=darwin-x86_64
HOST_NUM_CPUS=4
BUILD_NUM_CPUS=8
Auto-config: —arch=arm
ERROR: Failed to create toolchain.
When trying to build the toolchain arm I am getting this error. Why is that?
This comment has been minimized.
Copy link Quote reply
jainabhinav249 commented Mar 5, 2019
i am using android-ndk 14 and i am getting the clang:error: unknown argument: ‘-mandroid’ can any one help me
This comment has been minimized.
Copy link Quote reply
jainabhinav249 commented Mar 5, 2019
i an trying to build android-27 version
This comment has been minimized.
Copy link Quote reply
w1252675615 commented Sep 29, 2019
hello, i got «ignoring DT_PREINIT_ARRAY in shared library» when run my app on my phone, can you offer me some help or suggestion? thanks
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.