Cannot build (ld.exe: cannot find -lgcc_eh: No such file or directory)

I have created a hello world rust project that would simply print "hello world". However, it cannot be compiled and here is the error:

= note: C:\w64devkit\bin/ld.exe: cannot find -lgcc_eh: No such file or directory
          collect2.exe: error: ld returned 1 exit status

After I try to search for the solution on web, I noticed the gcc_eh library exists in "C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\self-contained" folder.

So I tried to put the following text in "C:\Users<username>.cargo\config.toml"

[target.x86_64-pc-windows-gnu]
rustflags = ["-L", "C:/Users/<username>/.rustup/toolchains/stable-x86_64-pc-windows-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/self-contained"]

After that, it compiles the hello world program successfully.

I am wondering if rust is supposed to link with the "self-contained" folder, or I suppose has gcc_eh library somewhere?

this is definitely not how it should behave. the rustc compiler driver is supposed to take care of all the toolchain internals for you. do you have any uncommon configurations? how did you create your project? what command did you run to build the program?

Here is my rustup (by running "rustup show"):

> rustup show
Default host: x86_64-pc-windows-gnu
rustup home:  C:\Users\<myusername>\.rustup

installed targets for active toolchain
--------------------------------------

aarch64-unknown-linux-gnu
x86_64-pc-windows-gnu

active toolchain
----------------

stable-x86_64-pc-windows-gnu (default)
rustc 1.69.0 (84c898d65 2023-04-16)

and my hello world project is simply created by typing the following commands:
(I have formatted the output a little bit)

> cargo new hello_world
     Created binary (application) `hello_world` package
> cd .\hello_world\
> cargo build
   Compiling hello_world v0.1.0 (H:\sandbox_folder\rust_sandbox\hello_world)
error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
  |
  = note: "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" 
          "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" 
          "C:\\Users\\<username>\\AppData\\Local\\Temp\\rustcr09JmV\\symbols.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.29s77toqe4ttu7xv.rcgu.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.46zhnv0l9ur81bku.rcgu.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.4dz6cvyf2pcw3qqa.rcgu.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.4so8ro1v9amwsq6v.rcgu.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.grlx72cgvobhfkj.rcgu.o" 
          "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.f3jmy2ok3hqd9a.rcgu.o" 
          "-L" "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps" 
          "-L" "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" 
          "-Wl,-Bstatic" "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libtest-e44a58b4ed5f47a2.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgetopts-7ed916f9715c8689.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunicode_width-4d96fcab4c17c3aa.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_std-dca0552393f82a4d.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-4208b5b050761eab.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-c24e86db72ac50d0.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-8130621e6418bc58.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libmemchr-08ae56106143c1db.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-ccfb784edb974a4a.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-4ab6c884d279676f.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-9ddceef41b9e9295.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_detect-d8366e410ad5f9d7.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-ce69f37d51637f0b.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libminiz_oxide-19f6886d9ebc20bf.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libadler-25358c2b66d0aa3d.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-d18d5ff4d2dae958.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-0cef8242de7339bc.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-edcd03acb0b5d165.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-21b2a42330a7f2e3.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-ee725b426657593b.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-d6f1a8662e8b71a9.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-84ddc19bfcf64c94.rlib" 
                         "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-6b61207aa476e831.rlib" 
            "-Wl,-Bdynamic" "-lkernel32" "-lkernel32" "-ladvapi32" "-luserenv" "-lkernel32" "-lws2_32" 
                            "-lbcrypt" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" 
                            "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" 
            "-Wl,--nxcompat" 
            "-L" "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" 
            "-o" "H:\\sandbox_folder\\rust_sandbox\\hello_world\\target\\debug\\deps\\hello_world-c018e8cfdcb29f8a.exe" 
            "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" 
            "C:\\Users\\<username>\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: C:\w64devkit\bin/ld.exe: cannot find -lgcc_eh: No such file or directory
          collect2.exe: error: ld returned 1 exit status

oh, I see, the linker invoked is not the one bundled with rust toolchain. probably the w64devkit mingw installation is not combatible with x86_64-pc-windows-gnu. I'd suggest you try to:
a) either update your mingw installation
b) modify your PATH environment variable to remove the w64dev in the shell where you build rust projects
c) manually configue cargo to use the bundled rust-lld as linker, I think this should work, but I didn't test it.

also, you can check #39915 and related discussions.

1 Like

Thanks! I configure cargo to use the rust-lld as linker, and now it works!

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.