rust 在 x86_64 机器上编译 x86 库

问题描述 投票:0回答:2

我有 ubuntu x86_64 容器,并且

cargo build
运行良好。 但我也需要构建 x86 库版本。 据我了解,我需要添加 i686 工具链和目标。

rustup target add i686-unknown-linux-gnu done successful
rustup toolchain install stable-i686-unknown-linux-gnu finished with error
$ rustup toolchain install stable-i686-unknown-linux-gnu
info: syncing channel updates for 'stable-i686-unknown-linux-gnu'
info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11-07)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'

  stable-i686-unknown-linux-gnu installed - (error reading rustc version)

$ rustup  default stable-i686
info: using existing install for 'stable-i686-unknown-linux-gnu'
info: default toolchain set to 'stable-i686-unknown-linux-gnu'

  stable-i686-unknown-linux-gnu unchanged - (error reading rustc version)

我是否错过了什么或采取了错误的方法?

rust cross-compiling rust-cargo
2个回答
3
投票

您不必更改工具链,而是必须将目标添加到当前工具链(确保首先切换回原始工具链)。

$ rustup target install i686-unknown-linux-gnu
$ cargo build --target=i686-unknown-linux-gnu

当然,您还需要在系统上安装 32 位库,例如在 ubuntu 上你可以通过

安装它们
$ sudo apt install gcc-multilib

(有关更多信息,请参阅如何在 64 位 Ubuntu 上编译 32 位应用程序?


0
投票

底部链接是一篇很棒的文章。而且

cargo
让一切变得如此简单。

cargo new --bin rust
     Created binary (application) `rust` package

cd rust
cargo run

   Compiling rust v0.1.0 (/home/user/Documents/rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.37s
     Running `target/debug/rust`
Hello, world!

cat ~/.cargo/config
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: /home/user/.cargo/config
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ [target.x86_64-unknown-linux-musl]
   2   │ linker = "x86_64-linux-gnu-gcc"
   3   │ 
   4   │ [target.x86_64-unknown-linux-gnu]
   5   │ linker = "x86_64-linux-gnu-gcc"
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

cargo run
   Compiling rust v0.1.0 (/home/user/Documents/rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/rust`
I love programming.

cargo build --release --target x86_64-unknown-linux-musl
   Compiling rust v0.1.0 (/home/user/Documents/rust)
    Finished release [optimized] target(s) in 0.31s
© www.soinside.com 2019 - 2024. All rights reserved.