dxatgp02
V2EX  ›  Rust

请教 rust 如何做到交叉编译

  •  1
     
  •   dxatgp02 · Jan 22, 2025 · 3703 views
    This topic created in 656 days ago, the information mentioned may be changed or developed.

    有一些写好的 rust 想放到 raspi 上面跑试试 网上查了"rust cross compile arm64" 用过

    cargo build --target aarch64-unknown-linux-gnu -r
    

    也试过 cargo 配置文件里加

    [target.aarch64-unknown-linux-gnu]
    linker = "aarch64-linux-gnu-gcc"
    rustflags = ["-L", "/usr/lib/gcc/aarch64-linux-gnu/13.1.0/tmp/usr/lib/"]
    

    都解决不了,放到 raspi 编译一次要很久。

    8 replies  •  2025-08-27 22:35:39 +08:00
    tangzs
    tangzs
    Jan 22, 2025❤️1
    1
    安装软件包
    ```
    sudo apt-get install gcc-multilib g++-multilib
    ```
    cargo 配置文件
    ```
    TRIPLET=aarch64-unknown-linux-gnu
    rustup target add $TRIPLET
    cargo build \
    --target $TRIPLET \
    --config target.$TRIPLET.linker=\"aarch64-linux-gnu-gcc\"
    ```
    静态编译
    ```
    TRIPLET=aarch64-unknown-linux-gnu
    rustup target add $TRIPLET
    RUSTFLAGS='-C target-feature=+crt-static' cargo build \
    --target $TRIPLET \
    --config target.$TRIPLET.linker=\"aarch64-linux-gnu-gcc\" \
    --release
    ```
    Nugine0
    Nugine0
    Jan 22, 2025❤️1
    2
    基于 docker 的一键交叉编译 https://github.com/cross-rs/cross

    最近 github actions 出了免费的 arm64 linux runner ,也可以白嫖
    he8822185
    he8822185
    Jan 25, 2025❤️1
    4
    用 cargo-zigbuild
    bli22ard
    bli22ard
    Feb 7, 2025❤️1
    5
    https://www.v2ex.com/t/1096658#r_15657744 17 楼的回答,rust 交叉编译最佳实战
    JohnSmith
    JohnSmith
    Feb 10, 2025 via Android❤️1
    6
    报错信息至少贴出来
    dxatgp02
    dxatgp02
    OP
    Feb 15, 2025
    7
    @JohnSmith 用上面的 zigbuild 能解决了
    Loocor
    Loocor
    Aug 27, 2025
    8
    @he8822185 这个是正解,大部分场景下应该是最简最佳方案
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4745 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 03:11 · PVG 11:11 · LAX 19:11 · JFK 22:11
    ♥ Do have faith in what you're doing.