Ld: library not found for -lSystem on a Mac Monterrey

Hi there,
I've installed rust following this guide: Install Rust - Rust Programming Language
I'm trying to build the Hello World app in Hello, World! - The Rust Programming Language
And I'm getting this error below.

Among the things I've tried, I've uninstalled rust and reinstalled it using brew.

(base) ➜  hello_world rustc main.rs
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "main.main.cbc7ddee-cgu.0.rcgu.o" "main.main.cbc7ddee-cgu.1.rcgu.o" "main.main.cbc7ddee-cgu.2.rcgu.o" "main.main.cbc7ddee-cgu.3.rcgu.o" "main.main.cbc7ddee-cgu.4.rcgu.o" "main.main.cbc7ddee-cgu.5.rcgu.o" "main.main.cbc7ddee-cgu.6.rcgu.o" "main.main.cbc7ddee-cgu.7.rcgu.o" "main.4kqwyhdgeomtzpxx.rcgu.o" "-L" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libstd-c6362a5f9528007e.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-196e36231af083e9.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libobject-f420fd3f10aa8205.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-22bb8a12823e5d13.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-2ca562738f920b27.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libgimli-044b3498ce18eb97.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-16174d1d9a37923a.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-ef53b0f9a8e337d5.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-1a08fa1ddf410423.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-77f44cf6bc4a11f0.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libunwind-5602d336b20745db.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-1902a2b406a30b1f.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/liblibc-9b59e472750d04d2.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/liballoc-30cc684d96e2be8f.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-a855ec16c5b04861.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libcore-5c36e9d724b8bf33.rlib" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-8fcd115ffa1f34e1.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "/usr/local/Cellar/rust/1.57.0/lib/rustlib/x86_64-apple-darwin/lib" "-o" "main" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: ld: library not found for -lSystem
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

cc and ld are on my path:

# ld -v
@(#)PROGRAM:ld  PROJECT:ld64-650.9
BUILD 00:19:30 Mar 17 2021
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 12.0.5, (clang-1205.0.22.9) (static support for 27, runtime is 27)
TAPI support using: Apple TAPI version 12.0.5 (tapi-1205.0.7.1)

# which cc
/Library/Developer/CommandLineTools/usr/bin/cc

# l /Library/Developer/CommandLineTools/usr/bin/cc
lrwxr-xr-x  1 root  wheel     5B Apr 28  2021 /Library/Developer/CommandLineTools/usr/bin/cc -> clang

# which ld
/Library/Developer/CommandLineTools/usr/bin/ld

# l /Library/Developer/CommandLineTools/usr/bin/ld
-rwxr-xr-x  1 root  wheel   5.5M Apr 20  2021 /Library/Developer/CommandLineTools/usr/bin/ld

# which rustc
/usr/local/bin/rustc

# l /usr/local/bin/rustc
lrwxr-xr-x  1 [username]  admin    31B Jan  3 09:56 /usr/local/bin/rustc -> ../Cellar/rust/1.57.0/bin/rustc

# rustc --version
rustc 1.57.0

Any ideas? Thanks.

I could make it work by specifying the library path to it. This works:

# rustc -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib ./main.rs
2 Likes

I've had exactly this same issue for ages, and haven't been able to work out how to fix it. Specifying the library path works perfectly; thank you.

Does anyone know how to fix this more generally, i.e. to tell Rust where to look for the library universally?

Ok, I've found that the longer-term fix for this is to add the following two lines to your .zshrc or equivalent:

export PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib:$PATH
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib" # Needed for Rust compilation and linking

rustc main.rs should then work as normal. That was painful... thanks Apple.

Edit: A friend points out that it's most likely related to this issue, i.e. when Apple moved to virtual libs.
Edit 2: Added that you should also add it to your path (not just LIBRARY_PATH).

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.