40

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.

DrYap
  • 6,525
  • 2
  • 31
  • 54
Incerteza
  • 32,326
  • 47
  • 154
  • 261

7 Answers7

61

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
DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
9

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)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Russo
  • 2,186
  • 2
  • 26
  • 42
  • 2
    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` refer to the solutions here - https://stackoverflow.com/q/16710047/11565176 .In my case I was missing the library all together and so this answer helped me - https://stackoverflow.com/a/42339395/11565176 – nsrCodes Nov 22 '21 at 15:39
  • 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 – Kelvin Ukuejubola Oritsetimeyi Jun 08 '23 at 04:16
9

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

Pat
  • 627
  • 1
  • 9
  • 24
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.

DrYap
  • 6,525
  • 2
  • 31
  • 54
  • Spot on. I had a very similar problem, and this answer was helpful in narrowing down the error message. – Alex Aug 03 '20 at 14:08
5

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

xcode-select --install
syned
  • 2,201
  • 19
  • 22
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.

gdev
  • 11
  • 1
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

YugoAmaryl
  • 363
  • 5
  • 15