'Unable to compile Rust hello world on Windows: linker link.exe not found

I have installed Rust on windows from Rust installation page. After installation I tried running the "hello world" program but got the following error.

>cargo run

Error

Compiling helloworld v0.1.0 (C:\Users\DELL\helloworld)

error: linker `link.exe` not found
note: The system cannot find the file specified. (os error 2)
note: the msvc targets depend on the msvc linker but `link.exe` was not found
note: please ensure that VS 2013, VS 2015 or VS 2017 was installed with the Visual C++ option
error: aborting due to previous error
error: Could not compile `helloworld`.

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

Code:

fn main() {
    println!("Hello, world!");
}


Solution 1:[1]

I downloaded and installed the Build Tools for Visual Studio 2019. During installation I selected the C++ tools. It downloaded almost 5GB of data. I restarted the machine after installation and compiling the code worked fine:

> cargo run
Compiling helloworld v0.1.0 (C:\Users\DELL\helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 12.05s
  Running `target\debug\helloworld.exe`
Hello, world!

Solution 2:[2]

I had a similar issue "error: linking with link.exe failed: exit code: 1"

To solve it, I did

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

then

rustup default stable-x86_64-pc-windows-gnu

and

cargo build
  Compiling hello v0.1.0 (C:\Users\leke\dev\rust\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.66s

Solution 3:[3]

Case 1: Using C++ win compiler, to fix it you need to reinstall VS build tool C++ Download the Visual Studio 2019 Build tools from the Microsoft website: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16

After the download, while installing the Build tools, make sure that you install the required components:

  1. C++ build tools

This will download required files. Once everything is successfully installed, reboot and re-run your rust program and it will compile successfully.

Case2: This error can come from the fact that you use GCC to compile, to fix it (assume that you already have MinGW):

Tape in cmd:

  1. rustup uninstall toolchain stable-x86_64-pc-windows-msvc
  2. rustup toolchain install stable-x86_64-pc-windows-gnu (or download rustup-init for the platform of your choice at https://forge.rust-lang.org/infra/other-installation-methods.html)
  3. rustup default stable-x86_64-pc-windows-gnu

Case 3: You don't want to download Visual studio with build tools, simply install MinGw with g++ gcc development packages, then run CASE 2

Solution 4:[4]

The error message is very unclear because there is no need to have Vistual Studio installed to run rust code. And what does "Visual C++ option" mean exactly? "VS 2013, VS 2015 or VS 2017" is also wrong - there's no need to install the full Visual Studio of these particular versions.

To execute 'cargo run' you need to install C++ build tools from Build Tools for Visual Studio. Version 2019 is just fine. Download link: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16#

It's important to select not only the default 'included' C++ tools during the installation but also three 'optional' C++ building tools: MSVC(...), Windows 10 SDK, C++ CMake tools for Windows.

Solution 5:[5]

Okay!

This is exactly what I did. Go to https://visualstudio.microsoft.com/visual-cpp-build-tools/ and it will download a Visual Studio Installer.

Run it as Administrator, then make sure you download all three things listed below in the screenshot, versions don't matter, just try get latest ones.

required list

Hit Install. Reboot Computer. You are welcome.

Solution 6:[6]

If the above solutions still do not work for you (this is 2021), Rust uses msvc and gnu compilers so you can always switch to the gnu compiler:

$ rustup default stable-x86_64-pc-windows-gnu

Solution 7:[7]

I had the same issue and found it to be present even after installing the Build Tools. What I realized almost by accident that I was running all my cargo commands in "Developer Command Prompt for Visual Studio ". Running the same commands in a simple cmd shell ran without any issues.

What worked for me: Running the command prompt directly and not use the shortcuts created by Visual Studio.

Possible Cause: Visual Studio Command Prompt runs bat files e.g. VsDevCmd.bat before it starts the shell (to load VS related environment variables, etc.) and possibly one of the commands in that file screws up the path cargo uses to get to linker.

Someone could dig further to find the exact line that causes the issue if they really want to know.

Solution 8:[8]

Try using Powershell outside Visual Studio, instead.

Then cargo run in src's parent folder.

You can try also: rustc

Good luck.

Solution 9:[9]

Just install Microsoft c++ Build Tools and you are good to go.

here is the link

https://visualstudio.microsoft.com/visual-cpp-build-tools/

Solution 10:[10]

I had some variables from an old Visual Studio installation in my System Variables. Removing those solved the issue.

VCHOME            C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
VCINSTALLDIR      C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
VS140COMNTOOLS    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common Tool...
vsinstalldir      C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC

Solution 11:[11]

Adding C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64 to PATH variable done the trick