148

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!");
}
Zobia Kanwal
  • 4,085
  • 4
  • 15
  • 38
  • 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 Apr 10 '19 at 08:24
  • 9
    Yes, thought it could be useful to others, hence shared the question with its answer. – Zobia Kanwal Apr 10 '19 at 13:15
  • 1
    @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 Sep 30 '21 at 01:52
  • @Merk *"[...] was installed with the Visual C++ option"* I mean... it doesn't tell you to install everything, does it? – hellow Sep 30 '21 at 06:07
  • 1
    @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 Oct 07 '21 at 15:45

16 Answers16

129

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!
Zobia Kanwal
  • 4,085
  • 4
  • 15
  • 38
  • 22
    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 May 15 '20 at 19:48
  • 1
    Thank you very much. – Al Fahad Jun 25 '20 at 03:39
  • 24
    @argenkiwi I was able to make it work with just "MSVC v142 - VS 2019 C++ x64/x86 build tools" and "Windows 10 SDK". – Fasani Jun 30 '20 at 23:55
  • Isn't there any other alternative? – lousycoder Jul 24 '20 at 17:48
  • 11
    I experimented with just the C++ build tools and it didn't work - you need the Windows 10 SDK as well. – derekdreery Nov 15 '20 at 16:33
  • When installing the C++ tools using VS build tools, I left all checkboxes checked. Although the Windows 10/11 SDK box was unchecked, I noticed it was installed during the installation. And the rust code works for me after I restart my laptop. – Yuchen Jun 14 '22 at 14:55
  • The fact that we have to install 5GB of build tools, just to compile "Hello, world" on Windows, is beyond ridiculous. Linux FTW – Trevor Sullivan Jul 10 '23 at 23:17
112

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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
the artist
  • 1,343
  • 1
  • 8
  • 4
  • 12
    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 Nov 13 '20 at 17:03
  • for windows 32 bit systems you will need `"stable-i686-pc-windows-gnu"` instead – Ahmed Jul 14 '21 at 01:59
  • 4
    @LuisAyuso Your comment would have been helpful if you listed which side effects you were thinking of. – Hashim Aziz Dec 16 '21 at 00:02
  • This worked for me! Installing the MS C++ tools didn't do anything, but running this afterwards fixed it. – Steve Dec 29 '21 at 00:46
  • 7
    @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 Jan 12 '22 at 12:30
  • 1
    This cannot be used with [Rocket](https://rocket.rs/) because it requires a nightly version of Rust and running `rustup default nightly` overrides the commands above. – Marco Lackovic Oct 09 '22 at 20:54
  • @MarcoLackovic Can't you tell rustup to download a nightly of the GNU-version? – Jaroslav Záruba Dec 04 '22 at 22:56
  • @MarcoLackovic I just tried `rustup default nightly-x86_64-pc-windows-gnu`, it downloaded stuff so I assume you can use this workaround with Rocket...? – Jaroslav Záruba Dec 04 '22 at 23:02
59

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

SinusQuell
  • 19
  • 7
ocanis
  • 607
  • 5
  • 3
27

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.

Rahul Bali
  • 692
  • 10
  • 28
  • 3
    In the `Workloads` tab click on `Desktop development with C++` then you'll see the listing above in the right pane. – holzkohlengrill Jan 30 '22 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 Sep 05 '22 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`. – Hosein Rahnama Dec 09 '22 at 23:18
  • I also needed clang on Windows 11 which I installed using `choco install llvm` – Jan Aug 28 '23 at 14:09
19

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
Adminixtrator
  • 554
  • 5
  • 8
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.

filiphagan
  • 196
  • 2
  • 9
  • How is this answer different from the [currently accepted answer](https://stackoverflow.com/a/55603112/2189130)? The link is the same and the comments provide the minimal installation options required. – kmdreko Mar 20 '21 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 Mar 21 '21 at 15:26
6

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
Marco Lackovic
  • 6,077
  • 7
  • 55
  • 56
3

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

Yuriy Pelekh
  • 168
  • 7
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.

Late Nighter
  • 69
  • 1
  • 1
  • 6
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

Gagan Suie
  • 320
  • 4
  • 15
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/

TheEhsanSarshar
  • 2,677
  • 22
  • 41
0

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

MFaiqKhan
  • 53
  • 8
-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

FunctorPrototype
  • 1,173
  • 2
  • 12
  • 24
  • 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 Jun 22 '23 at 14:40
  • Most likely this only works if you don't want a reboot, but not sure ;-) – jaques-sam Jun 22 '23 at 14:42
-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
Ovidiu
  • 851
  • 5
  • 9
-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.

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