Error: "linker 'cc' not found" when cross compiling a rust project from windows to linux using cargo
Asked Answered
P

2

17

I have a basic rust/cargo project with a single main file and some basic dependencies. The cargo build command works fine when the target is not specified (I am using windows so it builds to windows), but when I try to cross compile the program to linux using cargo build --target=x86_64-unknown-linux-gnu or cargo build --target=x86_64-unknown-linux-musl, the process fails with the following error: linker 'cc' not found.

Does anyone have an idea how to get around this? Is there a specific linker I need to install?

Thanks.

Protactinium answered 4/9, 2020 at 10:56 Comment(0)
P
15

I've just figured it out.

It turns out you need to tell cargo to use the LLVM linker instead. You do this by creating a new directory called .cargo in your base directory, and then a new file called config.toml in this directory. Here you can add the lines:

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "linker-flavor=ld.lld"]

Then building with the command cargo build --target=x86_64-unknown-linux-musl should work!

Protactinium answered 4/9, 2020 at 11:36 Comment(1)
So the above did not work for me (I got an error about missing lld) however the below did work. [target.x86_64-unknown-linux-musl] linker = "rust-lld"Dariodariole
F
7

This post summarises the two responses of Yaxlat and Rory Sullivan - thank you both:

Create a file named config.toml in the newly created directory .cargo in your base directory.

This file has the following content:

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

Enter these two commands in the terminal

rustup target add x86_64-unknown-linux-musl

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

The executable binary for Linux is now in the target directory.

Forehead answered 26/4, 2023 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.