rust-lang/rust

dylib for target -musl

pashinin opened this issue · 4 comments

As it was said here #34987, we should have support for targets that use a dynamic musl (like alpine linux) in nightly rust.

I want to build .so file to use on Alpine (musl).

[lib]
crate-type = ["dylib"]   # with staticlib it compiles OK
cargo build --target=x86_64-unknown-linux-musl --release --manifest-path ./Cargo.toml
error: cannot produce dylib for ... as the target `x86_64-unknown-linux-musl` does not support these crate types
rustc -V
rustc 1.22.0-nightly (f1b5225e8 2017-10-01)

Can you try passing -C target-feature=-crt-static? See second subpoint of point 5 of #40113 (comment)

Magic, thank you. But I thought rustc or cargo will set what it needs to be able to compile.

There are still some rough edges with dynamic musl - it's essentially a tier 3 platform and there are possible issues with backwards compatibility (since it's historically unconditionally meant static linking).

That said, I'd (cautiously) say that I see no reason why a dylib can't default to dynamic linking given it just errors out at the moment - we'd welcome a PR to start a discussion about it!

For me, adding this to .cargo/config made dylib work for x86_64-unknown-linux-musl

# Cargo.toml

[lib]
crate-type = ["dylib"]
# .cargo/config

[target.x86_64-unknown-linux-musl]
rustflags = [
    "-C", "target-feature=-crt-static",
]

Then this worked

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