空崎日奈
384 字
2 分钟
Zerotermux-Arch linux运行NDK

前言
上篇文章讲了如何在termux
上快速安装arch linux
,然后就可以玩一些好玩的,Google发行的NDK并不支持ARM架构
下载NDK
在Android开发者网站上下载NDK,下载地址为:https://developer.android.com/ndk/downloads
然后解压到root目录下
unzip android-ndk-r27b-linux.zip
然后安装zip
工具链并新建一个test目录
pacman -S zip &&mkdir test && cd test && touch test.sh
并填入以下内容
#!/bin/shset -eu
TEST_DIR="$(dirname "$(realpath "$0")")"
## ndk clang resource dir, get by command: <ndk_root>/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --print-resource-dirRESOURCE_DIR="${TEST_DIR}/../android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/18/"
## ndk sysroot, typically <ndk_root>/toolchains/llvm/prebuilt/linux-x86_64/sysroot/SYSROOT="${TEST_DIR}/../android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/"
## Android target tripleTARGET=aarch64-linux-android21
if command -v clang; then CLANG=clangelif command -v zig; then CLANG="zig cc"else print "Cannot find clang or zig\n" >&2 exit 1fi
## These options are needed for llvmbox# -isystem "${SYSROOT}/usr/include/c++/v1" \# -isystem "${SYSROOT}/usr/include" \# -isystem "${SYSROOT}/usr/include/aarch64-linux-android"
mkdir -p "${TEST_DIR}/output"
echo "Test C compiler..."${CLANG} \ -B "${TEST_DIR}/bin" \ -resource-dir "${RESOURCE_DIR}" \ --sysroot="${SYSROOT}" \ --target="${TARGET}" \ -xc - \ "$@" \ -o "${TEST_DIR}/output/hello-c" \ <<-EOF #include <stdio.h>
int main() { printf("%s\n", "Hello, C!"); return 0; } EOF
echo "Test C++ compiler..."${CLANG} \ -B "${TEST_DIR}/bin" \ -resource-dir "${RESOURCE_DIR}" \ --sysroot="${SYSROOT}" \ --target="${TARGET}" \ -xc++ -lc++ - \ "$@" \ -o "${TEST_DIR}/output/hello-cpp" \ <<-EOF #include <iostream> using namespace std;
int main() { cout << "Hello, C++!\n"; return 0; } EOF
if command -v file >/dev/null; then file "${TEST_DIR}/output/hello-c" "${TEST_DIR}/output/hello-cpp"fi
{{< notice tip >}} 项目引用Android SDK
{{< /notice >}}
测试
在test
目录下执行sh test.sh
,会打印出以下日志
/bin/clangTest C compiler...Test C++ compiler.../root/test//output/hello-c:ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[xxHash]=10df539d438a8009, not stripped/root/test//output/hello-cpp: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[xxHash]=8ce9c24e480bcc9d, not stripped
附录
参考文献
版权信息
本文原载于 YumeYuka.plus,遵循 CC BY-NC-SA 4.0 协议,复制请保留原文出处。
Zerotermux-Arch linux运行NDK
https://yumeyuka.plus/posts/ndk_for_android/