Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 396
Yaxlat Avatar asked Sep 04 '20 10:09

Yaxlat


1 Answers

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!

like image 177
Yaxlat Avatar answered Nov 08 '22 04:11

Yaxlat