Can't Compile (via. IntelliJ) due to an error with 'link.exe'

Hi, I recently downloaded the Rust SDK and just started with it. As an IDE I am using IntelliJ Idea with the Rust plugin. I made a run configuration which runs 'cargo build' and 'cargo run' whenever I click the run configuration on my IntelliJ IDE.

This is the output that shows up when trying to run 'hello-rust'.

Error Output

C:/Users/MaxRodriguez/.cargo/bin/cargo.exe run --color=always
Compiling hello-rust v0.1.0 (C:\Users\MaxRodriguez\hello-rust)
error: linking with link.exe failed: exit code: 1181
|
= note: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\bin\HostX64\x64\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.3q24yo12vxymd466.rcgu.o" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.42sfnsqu3vi4fepr.rcgu.o" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.46tcucbu4d1on1b1.rcgu.o" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.4mbl5vx2rdbe89c7.rcgu.o" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.5ckghltdpazfm5bq.rcgu.o" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.6373az97mayty4h.rcgu.o" "/OUT:C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.exe" "C:\Users\MaxRodriguez\hello-rust\target\debug\deps\hello_rust.i06yibzhbf0m0ac.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\intrinsic.natvis" "/NATVIS:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\liballoc.natvis" "/NATVIS:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\libcore.natvis" "/NATVIS:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\libstd.natvis" "/LIBPATH:C:\Users\MaxRodriguez\hello-rust\target\debug\deps" "/LIBPATH:C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libstd-d2cb2280bcd44383.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libpanic_unwind-140520697b0936d7.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libhashbrown-a3e1790633df522a.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_alloc-2f29aa3beddef483.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libbacktrace-963dd21d9104800a.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_demangle-ac2d16dbeba7d44c.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libunwind-bab8a0038eeffc7d.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcfg_if-0a2aca5d715fa450.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liblibc-04cd9a6d769db645.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liballoc-69018e88568c263f.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_core-f33a85683612f403.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcore-a5f276186a1cbe1e.rlib" "C:\Users\MaxRodriguez\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcompiler_builtins-7c234882c060b305.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
= note: LINK : fatal error LNK1181: cannot open input file 'advapi32.lib'

error: aborting due to previous error

error: could not compile hello-rust.

To learn more, run the command again with --verbose.

Process finished with exit code 101

You could try this.

If you have previously installed Visual Studio (Build Tools), launch Visual Studio Installer and make sure it hasn't lost track of your Visual Studio install. (If it has, your only option is to manually delete it and reinstall it, as far as I can tell.) VSInstaller allows you to install VS at a non-default position, and even on a non-OS drive, but at least for me, this led to it forgetting where VS was approximately every week. And because even uninstalling VSInstaller and VSWhere still leaves bits of configuration in the registry, this issue was only fixed for me when I built a new computer with an entirely fresh install of Windows (with a reasonably sized OS drive) and installed VS in the complete default position.

1 Like

Yep! That fixed it! I Installed the 2019 VS C++ Build Tool and it compiled! :+1:
One question I do have is why does the rust compiler need to link with the C++ build tool? Is a build tool different from compiler?

If you install the windows-gnu toolchain, then you can build without VS build tools, and it will mostly just work.

However, if you want to use basically any shared library (such as Windows APIs), then you need to use Microsoft's tools to assemble the final binary. This isn't made available to users by default (like it typically is on Linuxes) because most users don't need to compile their own software.

The short TL;DR of it is that Rustc and LLVM can produce what are known as "object files", which are small bags of executable code, but the platform linker (link.exe from vstools) is required to turn it into an actual executable.

1 Like

Ah ok, makes sense! Thanks :slight_smile:

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.