在 Ubuntu 上用 Python 进行交叉编译,通常指两种情况:
下面分别说明常见做法。
sudo apt update
sudo apt install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgz
tar xf Python-3.11.6.tgz
cd Python-3.11.6
./configure \
--host=arm-linux-gnueabihf \
--build=x86_64-linux-gnu \
--prefix=/opt/python-arm \
--enable-shared \
--without-ensurepip
make -j$(nproc)
make install
生成的 Python 就在 /opt/python-arm
crossenv(最推荐)适合已有目标平台 Python
pip install crossenv
crossenv /path/to/target/python3 crossenv
source crossenv/bin/activate
pip install your_package
✅ 自动处理 setup.py / pyproject.toml
zig + cibuildwheel(现代方案)pip install cibuildwheel
CIBW_ARCHS=armv7l cibuildwheel
export CC=arm-linux-gnueabihf-gcc
export CROSS_COMPILE=arm-linux-gnueabihf-
python setup.py build_ext --plat-name=linux-armv7l
maturin(Rust + Python)maturin build --target armv7-unknown-linux-gnueabihf
auditwheelauditwheel repair xxx.whl
cannot run test program while cross compiling在 configure 加:
ac_cv_file__dev_ptmx=yes
ac_cv_file__dev_ptc=no
✅ 用 crossenv 或预编译 wheel
| 场景 | 推荐方案 |
|---|---|
| 编译 Python 解释器 | ./configure --host |
| 编译 C 扩展 | crossenv |
| 构建 wheel | cibuildwheel |
| Rust 扩展 | maturin |
如果你能说明:
我可以给你精确命令。