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

Compiler ErrorsInstallationRustLinker ErrorsBuild Tools

Compiler Errors Problem Overview


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!");
}

Compiler Errors Solutions


Solution 1 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

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 - Compiler Errors

Try using Powershell outside Visual Studio, instead.

Then cargo run in src's parent folder.

You can try also: rustc

Good luck.

Solution 9 - Compiler Errors

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

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

Solution 10 - Compiler Errors

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 - Compiler Errors

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

Solution 12 - Compiler Errors

Firstly, download Microsoft C++ Build Tools and install it. Then, install rustup-init.exe. And don't delete the first one after successfully install rust.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZobia KanwalView Question on Stackoverflow
Solution 1 - Compiler ErrorsZobia KanwalView Answer on Stackoverflow
Solution 2 - Compiler Errorsthe artistView Answer on Stackoverflow
Solution 3 - Compiler ErrorsocanisView Answer on Stackoverflow
Solution 4 - Compiler ErrorsfiliphaganView Answer on Stackoverflow
Solution 5 - Compiler ErrorsRahul BaliView Answer on Stackoverflow
Solution 6 - Compiler ErrorsAdminixtratorView Answer on Stackoverflow
Solution 7 - Compiler ErrorsLate NighterView Answer on Stackoverflow
Solution 8 - Compiler ErrorsFausto CariasView Answer on Stackoverflow
Solution 9 - Compiler ErrorsTheEhsanSarsharView Answer on Stackoverflow
Solution 10 - Compiler ErrorsOvidiuView Answer on Stackoverflow
Solution 11 - Compiler ErrorsFunctorPrototypeView Answer on Stackoverflow
Solution 12 - Compiler ErrorsYousef McGeeView Answer on Stackoverflow