60

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.

1
  • No idea why this worked--I got this to go away by updating Rust Commented Jun 25, 2023 at 20:26

11 Answers 11

88

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
Sign up to request clarification or add additional context in comments.

11 Comments

Whenever you update the Mac with the new major mac release, usually the XCODE toolchain gets outdated that's the root cause of getting this error. Reinstalling the latest Xcode most of the time fixed the issue.
The cargo config was the missing piece for me. Thank you!
thank you so much @denis. This saved me a ton of hours. Cargo config is the deal here
Cargo config solved my error, thank you!
The config worked for me too!
|
14

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)

2 Comments

This is not just specific to sqlite3. In general, if you have a compilation error ending with somthing like **note** /usr/bin/ld cannot find -l<nameOfTheLibrary> refer to the solutions here - stackoverflow.com/q/16710047/11565176 .In my case I was missing the library all together and so this answer helped me - stackoverflow.com/a/42339395/11565176
If you are having this error on a linus or wsl2 ubuntu, this works fine. I had the error when trying to install sqlite with diesel
11

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

1 Comment

This is what worked for me on my M1.
10

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

xcode-select --install

Comments

6

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.

1 Comment

Spot on. I had a very similar problem, and this answer was helpful in narrowing down the error message.
4

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

Comments

1

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.

Comments

0

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

Comments

0

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"]

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.