You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux构建服务器交叉编译多平台Rust-based Python Wheel

可以在Linux构建服务器生成多平台Python Wheel

完全可以在单台Linux服务器上,通过交叉编译生成适配Windows、Linux x86_64/ARM架构的Python Wheel,以下是具体操作步骤:

前置准备

  1. 安装依赖工具

    • 安装rustup和cargo:完成Rust基础环境搭建
    • 安装maturin:通过pip install maturin或Cargo安装
    • 安装交叉编译工具链:
      • 针对Windows:Debian/Ubuntu系统安装gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64
      • 针对Linux ARM64:安装gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
  2. 添加Rust交叉编译目标

    # Linux x86_64(原生架构,默认已存在,可跳过)
    rustup target add x86_64-unknown-linux-gnu
    # Linux ARM64
    rustup target add aarch64-unknown-linux-gnu
    # Windows x86_64
    rustup target add x86_64-pc-windows-gnu
    

1. 生成Linux x86_64 Wheel

这是Linux服务器的原生架构,直接执行:

cd your-rust-python-project
maturin build --release

生成的Wheel文件会存放在target/wheels目录下,默认符合manylinux兼容性标准。


2. 生成Linux ARM64(aarch64)Wheel

  1. 设置交叉编译工具链环境变量:
    export CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
    export CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
    
  2. 执行maturin交叉编译:
    maturin build --release --target aarch64-unknown-linux-gnu
    
  3. (可选)若需静态链接(避免系统依赖),可改用aarch64-unknown-linux-musl目标,需提前安装对应musl工具链,并在Cargo.toml中添加:
    [profile.release]
    panic = "abort"
    rustflags = ["-C", "link-arg=-static"]
    

3. 生成Windows x86_64 Wheel

  1. 设置mingw工具链环境变量:
    export CC_x86_64_pc_windows_gnu=x86_64-w64-mingw32-gcc
    export CXX_x86_64_pc_windows_gnu=x86_64-w64-mingw32-g++
    
  2. 执行交叉编译:
    maturin build --release --target x86_64-pc-windows-gnu
    
  3. 注意:为避免Windows端缺失DLL,建议启用静态链接,在Cargo.toml的release配置中添加:
    [profile.release]
    rustflags = ["-C", "link-arg=-static-libgcc", "-C", "link-arg=-static-libstdc++"]
    

关键注意事项

  • 多Python版本支持:若需生成适配不同Python版本的Wheel,可通过--interpreter参数指定,例如:
    maturin build --release --target x86_64-pc-windows-gnu --interpreter python3.8 python3.9
    
  • Wheel兼容性验证:生成后可检查Wheel文件名的平台标签,确保符合PEP 425标准
  • 测试:可使用QEMU模拟ARM环境测试Linux ARM64 Wheel,或在Windows机器上验证Windows Wheel的可用性

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 16:40:08