Cross Compiling C for Android
- To cross compile for android, you’ll need a recent version of the NDK linked and in your $PATH. Download it here: Download
- Next, Unzip it in some location, e.g. $HOME.
- Add this path to your $PATH, so you can use the android c compilers:
fish_add_path "$HOME/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/bin"
- Since Termux supports API 24, we set these variables:
set TARGET_API 24
set CC aarch64-linux-android$TARGET_API-clang
set CXX aarch64-linux-android$TARGET_API-clang++
set AR llvm-ar
set AS llvm-as
set LD ld
set STRIP llvm-strip
set RANLIB llvm-ranlib
set SYSROOT $ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
set CFLAGS "-fPIE -fPIC --sysroot=$SYSROOT"
set LDFLAGS "-pie -L$SYSROOT/usr/lib -llog"
- Then we compile:
$CC -o $BIN_NAME main.c