"link.exe" not found, despite Build Tools already installed

I started following the Rust book, and got stuck when it was time to compile with rustc. I ran the command, and got this error:
error: linker `link.exe` not found
This apparently happens if the Visual Studio 2019 Build Tools aren't installed, but the thing is, they are.
I have Visual Studio 2019 Community Edition and the Visual C++ Tools are installed. I even managed to find the directory where link.exe resides.
Is there a way to find out in which directory rustc looks for link.exe, and if I can change it?

Not sure why the linker is not found, but I believe as a fallback solution you could install the GNU toolchain (you just won't be able to link your Rust code with MSVC created code):

rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu

A workaround is to use the "Native Tools Command Prompt".

It is odd that it's not found. Rust asks the Visual Studio Installer where the binaries are.

1 Like

Both VC and MinGW come with *.bat file to set environment variables up. For VC it was something like vcvars*.bat and for MinGW it is mingwvars.bat.

Have you tried running BAT file in your terminal before compiling with Rust?

The compiler doesn't really look in a specific directory, your operating system has an environment variable called PATH which lists all directories to search when trying to run an executable. It sounds like the directory containing link.exe isn't on your PATH.

The sanity check to make sure link.exe is accessible is to open a new command prompt and run something like link.exe /help. This should show the help text if it's accessible, or an error along the lines of "command not found" if it's not.

The vcvars.bat script installed with Visual Studio just sets up these environment variables for your shell, and the "Native Tools Command Prompt" mentioned earlier just runs this script automatically on startup.

1 Like

This isn't quite true. Rust uses the cc crate to find the linker (see here if you're interested in the details).

This should always find link.exe if it was installed via the Visual Studio Installer. Hence my surprise that it wasn't work in this case for some reason.

1 Like

I tried using the "Native Tools Command Prompt", and this time link.exe did run, but then failed with this error:

error: linking with `link.exe` failed: exit code: 1181
... followed by a really long command with paths ...

  = note: LINK : fatal error LNK1181: cannot open input file 'advapi32.lib'


error: aborting due to previous error

Hmm, do you have the Windows SDK installed?

1 Like

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.