rust-cross / rust-musl-cross

Docker images for compiling static Rust binaries using musl-cross
MIT License
617 stars 68 forks source link

[Resolved] got `cc: error: unrecognized command-line option '-m64'` or `ld: unknown option: --as-needed` #72

Closed chaseSpace closed 1 year ago

chaseSpace commented 1 year ago

docker command:

docker run -it --rm -v "$(pwd)":/home/rust/src \
                -v "/Users/lei/.cargo/config:/root/.cargo/config" \
                -v "/Users/lei/.cargo/registry:/root/.cargo/registry" \
            messense/rust-musl-cross:x86_64-musl

Cargo.toml:

[target.x86_64-unknown-linux-musl] 
linker = "x86_64-unknown-linux-musl-gcc"

build command: cargo build --target x86_64-unknown-linux-musl, then i got err:

warning: unused manifest key: target.x86_64-unknown-linux-musl.linker  // why ?
...omit a lot
error: linking with `cc` failed: exit status: 1
...omit a lot
 = note: cc: error: unrecognized command-line option '-m64'
chaseSpace commented 1 year ago

I have been solved this problem, i will show process here.

Firstly, we need to adjust ~/.cargo/config file, add target part(also the most important part):

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

In my experience, this is a key step, its aim is tell rust compiler to use which linker for your target. And I tried add target info in Cargo.toml or add env info CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-musl-gcc as build command prefix, but these ways were useless in the container(They works in my mac local).

Secondly, create a alias for docker build command, the following is that i used:

$ alias rust_musl_builder='docker run -it --rm -v "$(pwd)":/home/rust/src \
                        -v "/Users/lei/.cargo/config:/root/.cargo/config" \
                        -v "/Users/lei/.cargo/registry:/root/.cargo/registry" \
                        -v "/Users/lei/.cargo/git:/root/.cargo/git" \
                    messense/rust-musl-cross:x86_64-musl'

# note: replace the`config` and `registry` paths to yours on the -v options above. That two -v options will provide 
# caching function to speedup next build.
# DONT USE $HOME expression, it may leads a permisson issue.

then execute it:

$ rust_musl_builder rustup default nightly && rustup target add x86_64-unknown-linux-musl && cargo build --target x86_64-unknown-linux-musl --release

Error tips recording on build

...omit a lot
ld: unknown option: --as-needed

OR

...omit a lot
cc: error: unrecognized command-line option '-m64'

These errors all are caused by incorrect linker setting basically.