I encountered an error when building with the x86_64-pc-windows-gnu toolchain. Who can assist me?

When building a project using the x86_64-pc-windows-gnu toolchain, an error occurs:

PS E:\Daily\Desktop\hello> cargo build --target=x86_64-pc-windows-gnu
Compiling hello v0.1.0 (E:\Daily\Desktop\hello)
error: linking with `C:\msys64\mingw64\bin\gcc.exe` failed: exit code: 1
|
= note: "C:\msys64\mingw64\bin\gcc.exe" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "C:\Users\AXH69\.rustup\toolchains\stable-
x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\rsbegin.o" "C:\Users\AXH69\AppData\Local\Temp\rustcxzitGJ\symbols.o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\
debug\deps\hello-9bbe4c028840d372.19m6pwdq3w7fawf9.rcgu.o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.20vd8t7a6aaak3td.rcgu.o" "E:\Daily\Desktop
\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.21gunfaq851zmvk4.rcgu.o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.2rlp77
ph9mnpflrn.rcgu.o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.3iajtln59dr1976v.rcgu.o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.52yb8x5xhb7rk88m.rcgu.o" "-L" "E:\Daily\Desktop
\hello\target\x86_64-pc-windows-gnu\debug\deps" "-L" "E:\Daily\Desktop\hello\target\debug\deps" "-L" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_
.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libpanic_unwind-17d59fcffbb1858c.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libobject-bbbe10091d0fd58d.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libmemchr-ff20eb48d2567174.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libaddr2line-030e3ef637a6c0e1.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libgimli-53e82cde34eb1119.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\librustc_demangle-8a1d947ff4a075c3.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd_detect-a6a7bc9844ea0859.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libhashbrown-a963c0ad0363ef1a.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\librustc_std_workspace_alloc-1bc86c6d4a47ec27.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libminiz_oxide-57b7601d31d3fe19.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libadler-5ea4baaad4eac651.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libunwind-504a546f931d1b95.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libcfg_if-c18dc5b831cc4c35.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\liblibc-3a48b664c1fa889c.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\liballoc-2552a9f38657c70e.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\librustc_std_workspace_core-dab1b9a2489b7d16.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libcore-d7fd3f351d39ae46.rlib" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\libcompiler_builtins-b44a9797859024b2.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-lkernel32" "- lws2_32" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib" "-o" "E:\Daily\Desktop\hello\target\x86_64-pc-windows-gnu\debug\deps\hello-9bbe4c028840d372.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-gnu\lib\rsend.o"
= note:

error: could not compile `hello` (bin "hello") due to 1 previous error

The above is the complete content of the console error report.

//main.rs
fn main() {
println!("Hello, world!");
}
//cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]
//.cargo/config.toml
[target.i686-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
linker = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.39.33519\bin\Hostx86\x86\link.exe"

[target.i686-pc-windows-gnu]
linker = "C:\DevKit\MinGW\bin\gcc.exe"
ar = "C:\DevKit\MinGW\bin\ar.exe"

[target.x86_64-pc-windows-gnu]
linker = "C:\msys64\mingw64\bin\gcc.exe"
ar = "C:\msys64\mingw64\bin\ar.exe"

I want to know why this error occurred? I couldn't find relevant information online, and I can build and run normally using x86_64-pc-windows-msvc

Can you run it with the RUSTC_LOG env var set to rustc_codegen_ssa::back::link? That will hopefully give some more insight about why the linker failed. In any case have you tried removing the

[target.x86_64-pc-windows-gnu]
linker = "C:\msys64\mingw64\bin\gcc.exe"
ar = "C:\msys64\mingw64\bin\ar.exe"

For the x86_64-pc-windows-gnu target should ship with enough of MinGW itself already to compile any project that doesn't need a C or C++ compiler. This includes a linker and runtime libraries.

1 Like

When I comment out the following content, that is, remove:

//[target.x86_64-pc-windows-gnu]
//linker = "C:\\msys64\\mingw64\\bin\\gcc.exe"
//ar = "C:\\msys64\\mingw64\\bin\\ar.exe"

Seems to have returned to normal and can normally perform cargo build --target=x86_64-pc-windows-gnu, but when I tried
cargo build --target=i686-pc-windows-gnu, the following content was prompted

PS E:\Daily\Desktop\hello> cargo build --target=i686-pc-windows-gnu
Compiling hello v0.1.0 (E:\Daily\Desktop\hello)
error: The linker `i686-w64-mingw32-gcc` is not found
|
= note: The program is not found.

error: Could not compile `hello` (bin "hello") due to 1 previous error.

Meanwhile, there is still a problem when using cargo build --target=x86_64-pc-windows-gnu in another project of mine, but for now let's discuss the current problem first.

But I can use this way though I'm not clear whether they are different:

C:\Users\AXH69\.rustup\toolchains\stable-x86_64-pc-windows-gnu\bin\cargo.exe build

I see the linker inputs contains this object:

so you are using the wrong host toolchain it seems. cross-compile from windows-msvc to windows-gnu might be possible in theory, but since they share the same host os and target os, the standard library might get confused (it is not designed to do this kind of "cross" compiling). you might get away with nightly -Z build-std flags though

but the real solution is: don't do cross compiling in this case, just use the native host toolchain. i.e.:

instead of

cargo build --target=x86_64-pc-windows-gnu

do this:

cargo +x86_64-pc-windows-gnu build

or, set the default toolchain:

rustup default x86_64-pc-windows-gnu
cargo build
2 Likes

Thanks, this is very helpful for me

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.