skeeto / w64devkit

Portable C and C++ Development Kit for x64 (and x86) Windows
The Unlicense
3k stars 211 forks source link

Error while using mingw as a linker for rust #52

Closed valium007 closed 1 year ago

valium007 commented 1 year ago

OS: Windows 11 Pro x64 Rust version: stable Rust target: x86_64-pc-windows-gnu mingw build: https://github.com/skeeto/w64devkit/releases/download/v1.18.0/w64devkit-1.18.0.zip

I have extracted the zip and added the binaries folder to my path. Compiling a simple hello world in rust gives this error

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\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\Akshit\\AppData\\Local\\Temp\\rustc48wzX5\\symbols.o" "test.test.46b17e96-cgu.0.rcgu.o" "test.test.46b17e96-cgu.1.rcgu.o" "test.test.46b17e96-cgu.2.rcgu.o" "test.test.46b17e96-cgu.3.rcgu.o" "test.test.46b17e96-cgu.4.rcgu.o" "test.test.46b17e96-cgu.5.rcgu.o" "test.25wfbagu9nfc8nrd.rcgu.o" "-L" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-58a0209155176750.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-d3a377f51a08e93b.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-2b68121d5c691854.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libmemchr-e02210087fd130a4.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-c97c235d6ae1d402.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-5103cff433e42c31.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-10733230008649ea.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_detect-eae72c8f03b720e5.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-4fe1ee9fb3f485f8.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libminiz_oxide-73030570e1b9e0b1.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libadler-2aebe113ee2dfa73.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-e87307e69a4828fe.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-948f80140ec626cf.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-e7aed41dc626fc71.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-6df096732a8d89e9.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-2177fe373b363538.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-01b9d3625613d463.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-1d81dec6a35b3eaf.rlib" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-fedbd73de1b5211b.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-luserenv" "-lkernel32" "-lws2_32" "-lbcrypt" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-o" "test.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "C:\\Users\\Akshit\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: C:\Users\Akshit\Downloads\w64devkit-1.18.0\w64devkit\bin/ld.exe: cannot find -lgcc_eh: No such file or directory
          collect2.exe: error: ld returned 1 exit status

error: aborting due to previous error
skeeto commented 1 year ago

It seems either the Rust compiler requires a specific kind of GCC build than is supplied by w64devkit, or it's just invoking GCC incorrectly. It assumes the presence of libgcc_eh, but this functionality is provided in libgcc.a. I build GCC for only "static" compilation so that as much of the runtime as possible is statically linked into binaries. (Though even still it dynamically links the system msvcrt.dll.) I'm guessing libgcc_eh.a is not present as a separate library because it's only for dynamic linking.

Perhaps there's a way to request static linking from Rust so that it won't try to link this non-existent library? Alternatively, as a workaround, create an empty libgcc_eh, since libgcc will resolves its symbols.

$ ar rc "$W64DEVKIT_HOME/$(gcc -dumpmachine)/lib/libgcc_eh.a"

If the use of "-lgcc_eh" is not a mistake, then presumably it's there for a reason — e.g. it expects dynamic linking — and so your build might be broken, particularly regarding C++ exceptions crossing DLL boundaries.

valium007 commented 1 year ago

Alternatively, as a workaround, create an empty libgcc_eh, since libgcc will resolves its symbols. $ ar rc "$W64DEVKIT_HOME/$(gcc -dumpmachine)/lib/libgcc_eh.a" If the use of "-lgcc_eh" is not a mistake, then presumably it's there for a reason — e.g. it expects dynamic linking — and so your build might be broken, particularly regarding C++ exceptions crossing DLL boundaries.

It seems like creating a symlink to libgcc.a from empty libgcc_eh.a works. The code has compiled without any errors