How to build a 32bit static binary with Rust?
Asked Answered
I

1

10

Using Creating a basic webservice in Rust and Taking Rust everywhere with rustup as documentation, I have managed to successfully compile a 64 bit static binary with Rust:

rustup target add x86_64-unknown-linux-musl
cargo build --target=x86_64-unknown-linux-musl

But I can't seem to find out how to build a 32bit static binary.

I did find a i686-unknown-linux-musl target when running rustc --print target-list, only to find out it is not available when running rustup target list.

Am I missing something something or it is not possible yet?

Intyre answered 17/5, 2016 at 13:4 Comment(2)
Is i686-unknown-linux-gnu working ?Orola
As far as I know, it will not statically link libc.Intyre
F
10

The std binaries for i686-unknown-linux-musl is only available on Rust 1.10 or newer. You can create a static binary for i686 with the following commands:

$ rustup default stable # stable must at least 1.10
$ rustup target add i686-unknown-linux-musl
$ cargo build --target i686-unknown-linux-musl

The generated binaries can be found on target/i686-unknown-linux-musl/debug/ or target/i686-unknown-linux-musl/release/.

We can check that the generated binary is static linked with ldd:

$ ldd target/i686-unknown-linux-musl/debug/main
not a dynamic executable
Fivespot answered 17/6, 2016 at 14:15 Comment(5)
I did try it (with rust 1.10 as stable), but it created a dynamically linked binary. Am I missing something ? The compilation was OK, but when I ran ldd on it I saw it was dynamically linked.Intyre
@AndreiOprisan Updated the answer.Fivespot
For a freshly generated project it works as you said, for a project with more dependencies it links dynamically. Should I paste the toml file ?Intyre
Should I paste the toml file? Yes, please.Fivespot
Here is a minimal project which has the issue: github.com/aoprisan/static-rust . I will try to remove them one by one to find the culprit.Intyre

© 2022 - 2024 — McMap. All rights reserved.