Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rust fail to link on Windows?

Tags:

gcc

rust

I downloaded and installed the Rust and Cargo nightlies on my machine, and used Cargo to generate a new binary project:

Cifram@Valyria ~
$ cargo new test --bin

Then I attempt to run this virgin project, again with a fresh, untarnished install of both Rust and Cargo, and get this:

Cifram@Valyria ~/test
$ cargo run --verbose
   Compiling test v0.0.1 (file:///C:/cygwin64/home/Cifram/test)
     Running `rustc C:\cygwin64\home\Cifram\test\src\main.rs --crate-name test --crate-type bin -g --out-dir C:\cygwin64\home\Cifram\test\target --dep-info C:\cygwin64\home\Cifram\test\target\.fingerprint\test-51757ad0485ed143\dep-bin-test -L C:\cygwin64\home\Cifram\test\target -L C:\cygwin64\home\Cifram\test\target\deps`
error: linking with `gcc` failed: exit code: 1
note: gcc '-m64' '-L' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib' '-o' 'C:\cygwin64\home\Cifram\test\target\test.exe' 'C:\cygwin64\home\Cifram\test\target\test.o' '-Wl,--whole-archive' '-lmorestack' '-Wl,--no-whole-archive' '-fno-lto' '-fno-use-linker-plugin' '-Wl,--gc-sections' '-static-libgcc' '-Wl,--enable-long-section-names' '-Wl,--nxcompat' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libnative-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libstd-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libsync-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\librustrt-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libcollections-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\liballoc-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libunicode-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\liblibc-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\librand-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libcore-4e7c5e5c.rlib' '-L' 'C:\cygwin64\home\Cifram\test\target' '-L' 'C:\cygwin64\home\Cifram\test\target\deps' '-L' 'C:\cygwin64\home\Cifram\test\.rust' '-L' 'C:\cygwin64\home\Cifram\test' '-Wl,--whole-archive' '-Wl,-Bstatic' '-Wl,--no-whole-archive' '-Wl,-Bdynamic' '-lws2_32' '-lcompiler-rt'
note: C:\cygwin64\home\Cifram\test\target\test.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

error: aborting due to previous error
Could not compile `test`.

Caused by:
  Process didn't exit successfully: `rustc C:\cygwin64\home\Cifram\test\src\main.rs --crate-name test --crate-type bin -g --out-dir C:\cygwin64\home\Cifram\test\target --dep-info C:\cygwin64\home\Cifram\test\target\.fingerprint\test-51757ad0485ed143\dep-bin-test -L C:\cygwin64\home\Cifram\test\target -L C:\cygwin64\home\Cifram\test\target\deps` (status=101)

I'm not sure why rustc is invoking gcc, since I understood it was built on top of LLVM. I do have gcc installed, through MinGW, so my first thought was that maybe that gcc install was interfering.

Cifram@Valyria ~
$ gcc -v
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)

So it's an up-to-date version of gcc. But still, it might not be what rustc is expecting. So I removed C:\MinGW\bin from my path and tried again, and got:

Cifram@Valyria ~/test
$ cargo run --verbose
   Compiling test v0.0.1 (file:///C:/cygwin64/home/Cifram/test)
     Running `rustc C:\cygwin64\home\Cifram\test\src\main.rs --crate-name test --crate-type bin -g --out-dir C:\cygwin64\home\Cifram\test\target --dep-info C:\cygwin64\home\Cifram\test\target\.fingerprint\test-51757ad0485ed143\dep-bin-test -L C:\cygwin64\home\Cifram\test\target -L C:\cygwin64\home\Cifram\test\target\deps`
error: linking with `gcc` failed: exit code: 1
note: gcc '-m64' '-L' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib' '-o' 'C:\cygwin64\home\Cifram\test\target\test.exe' 'C:\cygwin64\home\Cifram\test\target\test.o' '-Wl,--whole-archive' '-lmorestack' '-Wl,--no-whole-archive' '-fno-lto' '-fno-use-linker-plugin' '-Wl,--gc-sections' '-static-libgcc' '-Wl,--enable-long-section-names' '-Wl,--nxcompat' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libnative-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libstd-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\librand-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libsync-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\librustrt-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libcollections-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\liballoc-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\liblibc-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libunicode-4e7c5e5c.rlib' 'C:\Program Files (x86)\Rust\bin\rustlib\x86_64-w64-mingw32\lib\libcore-4e7c5e5c.rlib' '-L' 'C:\cygwin64\home\Cifram\test\target' '-L' 'C:\cygwin64\home\Cifram\test\target\deps' '-L' 'C:\cygwin64\home\Cifram\test\.rust' '-L' 'C:\cygwin64\home\Cifram\test' '-Wl,--whole-archive' '-Wl,-Bstatic' '-Wl,--no-whole-archive' '-Wl,-Bdynamic' '-lws2_32' '-lcompiler-rt'
note: ld: this linker was not configured to use sysroots

error: aborting due to previous error
Could not compile `test`.

Caused by:
  Process didn't exit successfully: `rustc C:\cygwin64\home\Cifram\test\src\main.rs --crate-name test --crate-type bin -g --out-dir C:\cygwin64\home\Cifram\test\target --dep-info C:\cygwin64\home\Cifram\test\target\.fingerprint\test-51757ad0485ed143\dep-bin-test -L C:\cygwin64\home\Cifram\test\target -L C:\cygwin64\home\Cifram\test\target\deps` (status=101)

Well, it's a somewhat different error, but not really any better. The same gcc command is failing, but, before the error was:

C:\cygwin64\home\Cifram\test\target\test.o: file not recognized: File format not recognized

And now it's:

ld: this linker was not configured to use sysroots

I don't find either of these terribly meaningful. I tried running "rustc main.rs" directly. Got the same errors (except it was "src\main.o" instead of "target\test.o"). Tried doing all of this from the native Windows commandline instead of cygwin. Same errors. So it doesn't seem to be Cargo or cygwin that's to blame. I also tried downloading the last "stable" release of Rust (0.12.0), and still got the same errors. I've googled for these errors, and come up empty. So I'm running out of ideas.

Help?

like image 480
Michael Powell Avatar asked Oct 22 '14 05:10

Michael Powell


People also ask

Why is my Rust not connecting?

You should restart the PC and router as the first approach to fix can't connect to the rust server. Reinstalling anti-cheat is a well-known method that may help. If your friends cannot connect to your Rust server, make sure the port-forwarding configuration is correct.

Why does my Rust keep disconnecting?

Right click on rust, click properties. Go to local files and click "verify integrity of game cache." This will double check all your files to make sure nothing is missing or corrupted. Hope it helps.

Does Rust require Visual Studio?

Use of the Microsoft C++ Build Tools, or Visual Studio Build Tools, requires a valid Visual Studio license (either Community, Pro, or Enterprise). We'll be using Visual Studio Code as our integrated development environment (IDE) for Rust, and not Visual Studio. But you can still install Visual Studio without expense.


2 Answers

Success! The problem, it turns out, is that I had also had ld installed, inside cygwin. So from what I can tell, it was using the version of gcc that ships as part of rustc, but that gcc was calling out to C:\cygwin64\bin\ld.exe, instead of it's own internal ld.

As soon as I removed ld, everything worked.

Mind you, this reinforces the point I made in my comment: Apparently I can't do Rust development and C/C++ development on the same machine at the same time. Even for the purpose of compiling C libraries I want to access in Rust, like SDL2 or GLFW. This is terribly inconvenient. If rustc needs specific versions of gcc and ld, and ships with the correct versions, it should not default to calling out to the system versions. If it needs to be possible to override it's gcc or ld versions, this should be done through an explicit commandline option.

Edit: Adding a missing "not" that inadvertently reversed the meaning of a sentence.

like image 66
Michael Powell Avatar answered Dec 22 '22 18:12

Michael Powell


As an alternative solution, I manipulated the path variable when calling Rust from my cygwin shell environment. The way I understand it, is Rust will use the system path, to search for gcc, and then try to use it. By manipulating the path to remove the GCC's not wanted, it will fall back, and use the one that the rust installer came with.

PATH='/c/Program Files/Rust/bin' cargo build

*Use the correct cygwin path to where you installed Rust. On my system, this is /c/Program...

like image 36
Kevin Nisbet Avatar answered Dec 22 '22 19:12

Kevin Nisbet