仅参考,python没成功,有些模块什么的没编译进去
# 解压工具链(假设已下载到当前目录)
需要去官网下
tar -xJvf gcc-linaro-14.0.0-2023.06-x86_64_arm-linux-gnueabihf.tar.xz
sudo mv gcc-linaro-14.0.0-2023.06-x86_64_arm-linux-gnueabihf /opt/gcc-arm
# 配置环境变量
echo 'export PATH=$PATH:/opt/gcc-arm/bin' >> ~/.bashrc
source ~/.bashrc
# 验证工具链
arm-linux-gnueabihf-gcc --version # 应显示 14.0.0
安装依赖库
sudo apt install build-essential libssl-dev zlib1g-dev \
libncurses5-dev libbz2-dev libreadline-dev libsqlite3-dev \
liblzma-dev tk-dev libffi-dev
下载并编译 Python 3.11.7(主机版)
wget https://www.python.org/ftp/python/3.11.7/Python-3.11.7.tgz
tar -xzf Python-3.11.7.tgz
cd Python-3.11.7
# 编译本机工具链组件
./configure --prefix=/opt/python-host
make -j$(nproc)
sudo make altinstall
添加环境
# 添加 Python 可执行路径
export PATH="/opt/python-host/bin:$PATH"
# 添加 Python 库路径(解决模块导入问题)
export PYTHONPATH="/opt/python-host/lib/python3.11/site-packages:$PYTHONPATH"
echo 'export PATH="/opt/python-host/bin:$PATH"' >> ~/.bashrc
echo 'export PYTHONPATH="/opt/python-host/lib/python3.11/site-packages:$PYTHONPATH"' >> ~/.bashrc
source ~/.bashrc
配置交叉编译参数
cd Python-3.11.7
rm -rf build/ # 清理旧编译文件
编译openssl、zlib、libffi
下载,解压
./config no-asm shared no-async --prefix=/opt/openssl-arm/install --cross-compile-prefix=arm-linux-gnueabihf-
一般需要删除-m64
# 批量替换-m64参数
sed -i 's/-m64//g' Makefile
# 检查以下参数段
grep -rn "CFLAGS=" Makefile # 删除-m64
grep -rn "CXXFLAGS=" Makefile
make -j$(nproc)
sudo make altinstall
--with-openssl=/home/pi/openssl-1.1.1w/install
指定prefix对应的路径
--with-build-python 以我这个为例,对应的是py3.11.7 x86 版本
静态编译
设置参数
export ZLIB_ROOT=/opt/zlib-arm #zlib路径
需要根据对应平台进行查询
ls /dev/ptmx 有就yes,ptc也一样
echo "ac_cv_file__dev_ptmx=yes" > config.site
echo "ac_cv_file__dev_ptc=no" >> config.site
export CONFIG_SITE=config.site
# 执行 configure
./configure --build=x86_64-linux-gnu --host=arm-linux-gnueabihf --prefix=/opt/python-arm --with-build-python=/opt/python-host/bin/python3.11 --with-openssl=/opt/openssl-arm --with-system-ffi=/opt/libffi-arm --disable-ipv6 --disable-shared --without-ensurepip CFLAGS="-I/opt/openssl-arm/include -I/opt/libffi-arm/include" LDFLAGS="-static -L/opt/openssl-arm/lib -L/opt/libffi-arm/lib -l:libssl.a -l:libcrypto.a -l:libffi.a" LIBS="-lm -ldl"
注意wit和LDFLAGS CFLAGSh的路径
make -j$(nproc)
sudo make altinstall
最终验证是否是静态
arm-linux-gnueabihf-objdump -p /opt/python-arm/bin/python3.11 | grep NEEDED
zlib、openssl、libffi 都需要有对应的.a
无输出就是静态
安装qemu,使用QEMU用户模式仿真
sudo apt install qemu-user-static
qemu-arm-static --version 查看版本
不能启用armhf架构,通过dpkg --print-foreign-architectures查看,
要不然会提示错误,比如qemu-user-static:armhf什么的错误,然后一些系统命令就会错误
测试
echo -e '#include <stdio.h>\nint main() { printf("Hello from ARM!\\n"); return 0; }' > test.c
需要静态编译
arm-linux-gnueabihf-gcc -static -o test_arm_static test.c
/usr/bin/qemu-arm-static ./test_arm_static
正常输出
如果是
arm-linux-gnueabihf-gcc -o test_arm test.c
则会提示错误
pi@pi-virtual-machine:~$ /usr/bin/qemu-arm-static ./test_arm
qemu-arm-static: Could not open '/lib/ld-linux-armhf.so.3': No such file or directory
快照或者备份一下
测试qemu调用py3.11 arm
# 通过环境变量强制加载
export LD_LIBRARY_PATH="/opt/python-arm/lib:$LD_LIBRARY_PATH"
qemu-arm-static -L /opt/python-arm/lib /opt/python-arm/bin/python3.11
如果提示
/opt/python-arm/bin/python3.11: error while loading shared libraries: libpython3.11.so.1.0: cannot open shared object file: No such file or directory
之类的
尝试安装
环境变量控制
通过dpkg参数限定安装路径,避免污染系统目录:
sudo dpkg --add-architecture armhf
sudo apt-get install -o Dir::Etc::TrustedParts="/var/lib/apt-arm" libc6:armhf
将上面变量写入文件
echo 'export LD_LIBRARY_PATH="/opt/python-arm/lib:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc
换源
sudo nano /etc/apt/sources.list
# x86_64 主仓库
deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted
deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted
# ARM 架构仓库
deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jam22-updates main restricted universe multiverse
deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
sudo apt-get update