187

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!");
}
5
  • 5
    The error message says exactly what to do: "note: please ensure that VS 2013, VS 2015 or VS 2017 was installed with the Visual C++ option"
    – hellow
    Commented Apr 10, 2019 at 8:24
  • 10
    Yes, thought it could be useful to others, hence shared the question with its answer. Commented Apr 10, 2019 at 13:15
  • 2
    @hellow not quite "exactly", that's the entire problem.... the Visual Studio installer has about 30 options and installing them all would probably require a terabyte of downloads. filiphagan below helpfully mentions the ones necessary.
    – Merk
    Commented Sep 30, 2021 at 1:52
  • 1
    @Merk "[...] was installed with the Visual C++ option" I mean... it doesn't tell you to install everything, does it?
    – hellow
    Commented Sep 30, 2021 at 6:07
  • 2
    @hellow "exactly" suggests "sufficient information to pick items from the list", which is not the case given the number of (multi-Gb) options with Visual C++ and VS 20xx in the list, as multiple answers below attest. A helpful comment on this question would help discriminate among them.
    – Merk
    Commented Oct 7, 2021 at 15:45

19 Answers 19

162

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
9
  • 15
    This is not a solution but a work-around, It is important to consider that you change the toolchain when you do this and there are side effects.
    – Luis Ayuso
    Commented Nov 13, 2020 at 17:03
  • for windows 32 bit systems you will need "stable-i686-pc-windows-gnu" instead
    – Ahmed
    Commented Jul 14, 2021 at 1:59
  • 8
    @LuisAyuso Your comment would have been helpful if you listed which side effects you were thinking of. Commented Dec 16, 2021 at 0:02
  • 8
    @HashimAziz The proposed solution changes the toolchain, from MSVC to GNU. It does not solve the original problem (possibly a MSVC faulty installation), Into using a completely different compiler and linker. Compatibility, features, and performance may not be the same. To better understand the difference, look for the implications of changing your windows development from MSVC to GNU.
    – Luis Ayuso
    Commented Jan 12, 2022 at 12:30
  • 1
    This cannot be used with Rocket because it requires a nightly version of Rust and running rustup default nightly overrides the commands above. Commented Oct 9, 2022 at 20:54
143

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!
8
  • 23
    Do we need to leave all of the default check-boxes checked? Otherwise, what would be the minimum set of dependencies we could install to get Rust to work?
    – argenkiwi
    Commented May 15, 2020 at 19:48
  • 30
    @argenkiwi I was able to make it work with just "MSVC v142 - VS 2019 C++ x64/x86 build tools" and "Windows 10 SDK".
    – Fasani
    Commented Jun 30, 2020 at 23:55
  • Isn't there any other alternative?
    – lousycoder
    Commented Jul 24, 2020 at 17:48
  • 13
    I experimented with just the C++ build tools and it didn't work - you need the Windows 10 SDK as well. Commented Nov 15, 2020 at 16:33
  • 1
    The fact that we have to install 5GB of build tools, just to compile "Hello, world" on Windows, is beyond ridiculous. Linux FTW Commented Jul 10, 2023 at 23:17
63

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):

Type 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

3
  • rustup uninstall toolchain stable-x86_64-pc-windows-msvc this command is throwing up Access denied error, not sure how many faced this issue. Though this answer is a very detailed one , i'm voting up for the below answer as it is more of quick fix. Commented Jul 25, 2021 at 6:20
  • Thanks! that solved the issue. I somehow misinterpreted the multiple warnings that rustup had on the need for VC++ build tools. Like it was optional or something hehe
    – Alex. S.
    Commented Sep 7, 2021 at 20:54
  • I did cases 1 & 2, and that worked for me. Thank you so much! Commented Feb 26, 2022 at 3:36
31

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.

4
  • 3
    In the Workloads tab click on Desktop development with C++ then you'll see the listing above in the right pane. Commented Jan 30, 2022 at 18:17
  • @holzkohlengrill i can confirm your solution also works for VS 2022. Tapping Desktop development with C++ should actually be the solution. It gives you everything you need.
    – Gagan Suie
    Commented Sep 5, 2022 at 19:18
  • 1
    Although in rust's installation instructions, it is said that the minimal requirements are MSVC and windows 10 SDK, it seems that is also requires C++ CMake tools for windows. Commented Dec 9, 2022 at 23:18
  • I also needed clang on Windows 11 which I installed using choco install llvm
    – Jan
    Commented Aug 28, 2023 at 14:09
22

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
1
  • 3
    my computer was dangerously low on disk space (i.e i couldn't install C++ build tools because it's 6 gb), you just saved me, thank you kind stranger :)
    – Rat
    Commented Jun 19, 2022 at 4:58
18

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.

2
  • How is this answer different from the currently accepted answer? The link is the same and the comments provide the minimal installation options required.
    – kmdreko
    Commented Mar 20, 2021 at 20:00
  • 5
    @kmdreko the answer is not solving the issue since other requirements like MSVC and Windows 10 SDK are not mentioned there
    – filiphagan
    Commented Mar 21, 2021 at 15:26
10

In a PowerShell terminal run:

winget install -e --id Microsoft.VisualStudio.2022.BuildTools

Then:

  • open Visual Studio Installer
  • press Modify
  • check Desktop Development with C++
  • press Modify
6

On Windows, this is fixed via

winget install Rustlang.Rust.GNU Rustlang.Rust.MSVC

1
  • 2
    This was my problem, I had installed only Rustlang.Rustup
    – luksfarris
    Commented Apr 19 at 13:34
5

I see that term C++ Build tools introduces confusion what exactly to select during the installation.

It is Desktop development with C++ component with default dependencies (see screenshot). From my experience I noticed that only two options are required (highlighted with red rectangles on the screenshot). But to be confident just go with default selection and enjoy :)

Desktop development with C++

2

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.

1
  • Thanks a lot! cargo run from the command line (instead of Command Prompt for VS) helped in my case. Commented Jun 12, 2020 at 8:11
2

On VS 2022, I tested both solutions.

4.39 GB = "MSVC v143 - VS 2022 C++ x64/x86 build tools" and "Windows 10 SDK"

2.86 GB = "Desktop development with C++"

Its better to just select "Desktop Development with C++".

Heres the download for VS 2022 build tools: https://aka.ms/vs/17/release/vs_BuildTools.exe

2

Method 1: If You Are Using MSVC ToolChain to Compile Your Rust Program:

Install Microsoft cpp Build tool, Install it through Administrator and reboot you pc , your rust program will run correctly https://visualstudio.microsoft.com/visual-cpp-build-tools/ .

Method 2: Use MINGW GCC Compiler , GNU based compiler and toolchain to compile your rust program

Install mingw by following this guide , https://code.visualstudio.com/docs/cpp/config-mingw

If you have MINGW than all you have to do is paste a config file containing this

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

in

C:\Users\yourname\.cargo\bin

Don't forget to make gnu your default rustup compiler

rustup default stable-x86_64-pc-windows-gnu

1
  • Confirmed working! Essencial when installing Rust without admin, since VIsual Studio related tools needs elevation. Commented Jun 7 at 14:10
1

I faced similiar issue instead of installing Visual studio build tools for C++, I compiled the project in WSL2, it might be similiar to installing mingwin + stable-x86_64-pc-windows-gnu.

Also using winget you can install rust both for msvc or gnu.

C:\Users\Name>winget search rust | find "Rust ("
Rust (MSVC)                          Rustlang.Rust.MSVC                 1.72.0             Tag: rust           winget
Rust (GNU)                           Rustlang.Rust.GNU                  1.72.0             Tag: rust           winget

References

  1. Rust installation on WSL2.
  2. Package installation using winget
0

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

here is the link

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

-1

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

2
  • Actually, this has worked for me 2 and I found this the only way to do it :-O I don't get the down votes! It's unfortunate that this is the way to find the linker because each time you update VS, you'll have to change the variable...
    – jaques-sam
    Commented Jun 22, 2023 at 14:40
  • Most likely this only works if you don't want a reboot, but not sure ;-)
    – jaques-sam
    Commented Jun 22, 2023 at 14:42
-1

Post VS installation & update in C++ build mode Note- Compilation needs this env.

cargo run - works

enter image description here

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 25, 2023 at 20:38
-3

Try using Powershell outside Visual Studio, instead.

Then cargo run in src's parent folder.

You can try also: rustc

Good luck.

-3

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

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.

1
  • 4
    It's unclear what this provides in addition to the accepted answer, which already explains to install the build tools.
    – Shepmaster
    Commented Jun 29, 2021 at 18:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.