384 字
2 分钟
Zerotermux-Arch linux运行NDK
2024-08-10

前言#

上篇文章讲了如何在termux上快速安装arch linux,然后就可以玩一些好玩的,Google发行的NDK并不支持ARM架构

下载NDK#

Screenshot_2024-09-24-19-54-23-824_com.microsoft.emmx-edit.jpg 在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/sh
set -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-dir
RESOURCE_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 triple
TARGET=aarch64-linux-android21

if command -v clang; then
	CLANG=clang
elif command -v zig; then
	CLANG="zig cc"
else
	print "Cannot find clang or zig\n" >&2
	exit 1
fi

## 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 >}} android-sndk.png

测试#

test目录下执行sh test.sh,会打印出以下日志

/bin/clang                                
Test 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   

附录#

参考文献#

NDK下载链接

Android SDK

版权信息#

本文原载于 nightrainmilkyway.cn,遵循 CC BY-NC-SA 4.0 协议,复制请保留原文出处。

Zerotermux-Arch linux运行NDK
https://fuwari.vercel.app/posts/ndk_for_android/
作者
YumeYuka
发布于
2024-08-10
许可协议
CC BY-NC-SA 4.0