Skip to content

Instantly share code, notes, and snippets.

@Tydus
Last active May 4, 2023 13:43
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Tydus/11109634 to your computer and use it in GitHub Desktop.
Save Tydus/11109634 to your computer and use it in GitHub Desktop.
How to install Standalone toolchain for Android

HOWTO Cross compiling on Android

5W1H

What is NDK

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:

  1. The program is standalone (has nothing connect to NDK, and don't need helper scripts to run it)
  2. 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:

/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/home/tyeken8/Desktop/elab/geo/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: error: ld returned 1 exit status

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.

Howto

  1. Download Android NDK
    from https://developer.android.com/tools/sdk/ndk/index.html
  2. Extract the NDK
    tar xf android-ndk-r9d-*.tar.bz2 && cd android-ndk-r9d
  3. Make GNU Android Toolchain from NDK
    build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.8 --platform=android-19 --install-dir=../toolchain
  4. Delete the NDK (Yes, we don't need it any more)
    cd .. && rm -rf android-ndk-r9d
  5. 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
  6. (Optional) Now you can use it as a standard GNU toolchain
    For example: ./configure --prefix=/opt/android --host=arm-linux-androideabi && make && make install
@nikhilkilivayil
Copy link

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

What will do?

@michaelsx
Copy link

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).

@usman313
Copy link

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??
untitled

@ranshalit
Copy link

That's amazing. Thank you very much. I don't have to build now with "-static". Right ?

@hannesa2
Copy link

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 ?

@AxelNennker
Copy link

@hannesa2
echo "main(){}" | ./arm-linux-androideabi-gcc -x c - && file a.out

@KrishnaCAmjuri
Copy link

KrishnaCAmjuri commented Jun 2, 2018

Is this procedure valid for mac(unix) also?

@unalkalkan
Copy link

Tried in Debian 9, works like a charm!

@KrishnaCAmjuri
Copy link

This will only build the toolchain for one architecture that is "armv7-a". What about the other architectures? Can you please clarify?

@bybilly
Copy link

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?

@tonyw5
Copy link

tonyw5 commented Aug 14, 2018

Tonys-MacBook-Pro:~ 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?

@jainabhinav249
Copy link

i am using android-ndk 14 and i am getting the clang:error: unknown argument: '-mandroid' can any one help me

@jainabhinav249
Copy link

i an trying to build android-27 version

@w1252675615
Copy link

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

@vivek-lakhataria
Copy link

i have issue in ndk-12b
command -: make TARGET=android-x86 build_spatialite build_gdal build_commoncommo build_assimp
make TARGET=android-x86 build_spatialite build_gdal build_commoncommo build_assimp
/home/yash/ndk/build/tools/make-standalone-toolchain.sh
--platform=android-21
--arch=x86
--install-dir=/home/yash/AndroidTacticalAssaultKit-CIV/takthirdparty/builds/android-x86-release/toolchain
make: /home/yash/ndk/build/tools/make-standalone-toolchain.sh: Command not found
make: *** [target-config/android-x86.mk:11: /home/yash/AndroidTacticalAssaultKit-CIV/takthirdparty/builds/android-x86-release/toolchain] Error 127

@Mohamedeijy
Copy link

This will only build the toolchain for one architecture that is "armv7-a". What about the other architectures? Can you please clarify?

Does someone have an answer concerning this ? Is it possible to specify other architectures ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment