Incerteza
Incerteza

Reputation: 34884

error: linking with `cc` failed: exit code: 1

I have a single .rs file. When I compile it by rustc test1.rs, I get an error:

    error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib' '-o' 'test1' 'test1.o' '-Wl,-force_load,/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a' '-Wl,-dead_strip' '-nodefaultlibs' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libcollections-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libunicode-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/librand-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e7c5e5c.rlib' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib/libcore-4e7c5e5c.rlib' '-L' '/usr/local/Cellar/rust/1.0.0-alpha/lib/rustlib/x86_64-apple-darwin/lib' '-L' '/Users/alex/Documents/projects/rust/.rust/lib/x86_64-apple-darwin' '-L' '/Users/alex/Documents/projects/rust/lib/x86_64-apple-darwin' '-lSystem' '-lpthread' '-lc' '-lm' '-lcompiler-rt'
note: ld: warning: directory not found for option '-L/Users/alex/Documents/projects/rust/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/alex/Documents/projects/rust/lib/x86_64-apple-darwin'
ld: can't open output file for writing: test1, errno=21 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error


$ rustc --version
rustc 1.0.0-dev

I've seen some topic related to this one but none of them helped me to solve the problem.

Upvotes: 60

Views: 96529

Answers (11)

bartonstanley
bartonstanley

Reputation: 1279

In my case, I was also seeing this:

  = note: ld: warning: search path '/opt/homebrew/Cellar/proj/9.4.1/lib' not found
          ld: warning: search path '"/opt/homebrew/Cellar/proj/9.4.1/lib"' not found
          ld: library 'proj' not found

What solved it for me was to run rustup update.

Upvotes: 0

DenisKolodin
DenisKolodin

Reputation: 15071

I was faced with three problems on Mac compiling Rust:

First: If you have any issue with writing files/dirs by ld just remove that files and try to recompile. I don't know why, but on Mac this issue happens time to time.

Second: If you have other ld errors (not about file access): try to add the following sections to your ~/.cargo/config (if you don't have this file feel free to create):

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

Third: Sometimes your Mac lack of some dev tools/dependencies. Install the most important of them automatically with the command:

xcode-select --install

Upvotes: 86

dnetto
dnetto

Reputation: 21

After updating the Mac M1, for some reason I don't know, I had the same error, but throwing:

$> ld

he told me it was because of the license, then just launch

$> sudo xcodebuild -license accept

Have a good day :D

Upvotes: 2

kenechukwu igweagu
kenechukwu igweagu

Reputation: 11

The IntelliJ terminal has to run with sudo privileges. Close IntelliJ and open with sudo "/Applications/IntelliJ IDEA 9.0.3.app/Contents/MacOS/idea" from the terminal.

Upvotes: 0

Larry Neil
Larry Neil

Reputation: 326

I faced this kind of error and fixed by installing xcode-select with this command.

xcode-select --install

FYI, I am working with macOS. And also added this to the Cargo.toml file like below:

[build]
rustc-args = ["-Vv"]

Upvotes: 0

Russo
Russo

Reputation: 2982

If you have note: /usr/bin/ld: cannot find -lsqlite3

then install libsqlite3-dev:

$ sudo apt install libsqlite3-dev

This works on Rust 1.53.0, Linux Mint 20.2(based on Ubuntu 20.04 LTS)

Upvotes: 13

YugoAmaryl
YugoAmaryl

Reputation: 403

In my case, I'm on a ubuntu22.04, I got error like following

error: linking with `cc` failed: exit status: 1
...
note: /usr/bin/ld: cannot find -lpython3.10: No such file or directory

I solved it by sudo apt install python3.10-dev

Upvotes: 0

gdev
gdev

Reputation: 11

I had the same issue recently and I found out this solution that worked for me:

https://www.docker.com/blog/cross-compiling-rust-code-for-multiple-architectures/

On Running Rust on aarch64 I found out that libc6-dev-arm64-cross is need in order to compile rust successfully on aarch64.

Upvotes: 1

syned
syned

Reputation: 2321

My rust project stopped building after updating my MacOS so this command fixed it for me

xcode-select --install

Upvotes: 10

Pat
Pat

Reputation: 668

If you have a MacBook M1(x) with ARM processor you need to install rust from rustup https://sourabhbajaj.com/mac-setup/Rust/

When you run rustup-init, use the customize option to change aarch64-apple-darwin to x86_64-apple-darwin

Then you can add the following to .cargo/config.toml or .cargo/config (either is fine)

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

This solution was tested with Rust 1.54 and MacBook M1

I was able to do a cargo build --release and generate a dylib file from this tutorial https://www.youtube.com/watch?v=yqLD22sIYMo

Upvotes: 11

DrYap
DrYap

Reputation: 6647

From your command rustc test1.rs the compiler infers the name of the executable should be test1. The linker tries to open this file so it can write the executable but fails with errno=21 whose stringified version is "Is a directory".

This suggests you have a directory in your working directory called test1 which is causing a conflict.

Upvotes: 6

Related Questions