21

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,345
  • 2
  • 29
  • 54
Incerteza
  • 27,981
  • 42
  • 142
  • 242

4 Answers4

28

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
  • 10,965
  • 3
  • 55
  • 62
5

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,345
  • 2
  • 29
  • 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
3

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)

Russo
  • 1,518
  • 1
  • 17
  • 34
  • 1
    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
0

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
  • 507
  • 1
  • 7
  • 23