Question
Fixing Rust "link.exe not found" on Windows: Understanding the MSVC Linker
Question
I installed Rust on Windows from the official Rust installation page. After installation, I tried to run a simple Hello World program, but cargo run failed with a linker error.
This is the command I ran:
cargo run
And this is the 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.
Here is the program:
fn main() {
println!("Hello, world!");
}
Why does this happen, and how can I fix it on Windows?
Short Answer
By the end of this page, you will understand why Rust on Windows sometimes fails with link.exe not found, what a linker does, how the MSVC Rust toolchain depends on Microsoft C++ build tools, and the practical ways to fix the problem so cargo run works correctly.
Concept
Rust compilation on Windows often involves more than just the Rust compiler.
When you run:
cargo run
Cargo does roughly two major jobs:
- Compiles your Rust source code into object files
- Links those object files into a final executable program
That second step is where your error happens.
What is a linker?
A linker is a tool that combines compiled pieces of code into a final .exe file. Rust can compile your code, but on Windows the final executable often depends on a platform-specific linker.
Why link.exe specifically?
Your installed Rust toolchain is likely the MSVC toolchain, such as:
x86_64-pc-windows-msvc
The msvc part means Rust expects to use Microsoft's build tools, including the Microsoft linker:
link.exe
If that linker is not installed or not available in your environment, compilation fails even for a tiny program like Hello World.
Why this matters
This is a common beginner issue on Windows because installing Rust alone may not be enough for the MSVC target. You also need the Windows-native C/C++ build tools that Rust relies on.
In real programming, this matters because:
- Rust projects may depend on native system libraries
- Cross-platform builds use different toolchains on different systems
- Build errors are often caused by missing external tools, not incorrect Rust syntax
So the problem is not your Rust code. The problem is that the build environment is incomplete for the toolchain you selected.
Mental Model
Think of building a Rust program like building a house:
- Rust compiler = workers who create the parts
- Object files = the walls, doors, and windows
- Linker = the crew that assembles everything into the final house
Your Hello World code is fine. The workers finished their part. But the final assembly crew is missing.
On Windows with the MSVC toolchain, that assembly crew is link.exe.
So link.exe not found means:
- Rust started the build
- Rust compiled the code
- Rust tried to assemble the final
.exe - The required Windows linker was not available
Syntax and Examples
Core idea
You write Rust code as usual:
fn main() {
println!("Hello, world!");
}
Then you build or run it with Cargo:
cargo build
cargo run
But on Windows, the build also depends on the selected target toolchain.
Check your Rust toolchain
Run:
rustup show
You may see something like:
Default host: x86_64-pc-windows-msvc
active toolchain
----------------
stable-x86_64-pc-windows-msvc (default)
If you see msvc, Rust expects Microsoft's linker tools.
Typical fix: install Visual Studio Build Tools
Install the Microsoft C++ build tools that include the linker.
Commonly needed components:
- Desktop development with C++
- MSVC build tools
- Windows SDK
After installation, open a new terminal and try:
cargo run
Alternative: use the GNU toolchain
In some setups, developers use the GNU toolchain instead of MSVC.
Example:
rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu
This uses a different linker toolchain, not link.exe.
Example workflow
rustup show
cargo build -v
If the output mentions windows-msvc and fails on link.exe, the issue is almost certainly missing MSVC build tools, not broken Rust source code.
Step by Step Execution
Consider this program:
fn main() {
println!("Hello, world!");
}
And this command:
cargo run
What happens step by step
1. Cargo reads your project
Cargo checks Cargo.toml and src/main.rs.
2. Rust compiles the source code
rustc turns your Rust code into compiled object files.
At this point, your source code is valid.
3. Cargo starts the linking stage
Rust now needs to create the final executable, such as:
helloworld.exe
4. Rust looks for the configured linker
Because your target is likely:
x86_64-pc-windows-msvc
Rust expects the Microsoft linker:
link.exe
5. The linker is missing
Windows returns:
The system cannot find the file specified. (os error 2)
That means the executable link.exe is not installed or not accessible from the current build environment.
6. Build stops
Since the final executable cannot be created, Cargo reports:
error: Could not compile `helloworld`.
Important takeaway
The Rust code is not the issue. The failure happens after compilation, during the final assembly step.
Real World Use Cases
This concept appears in many real Rust projects on Windows.
1. Building command-line tools
A simple CLI app may compile fine on Linux or macOS but fail on Windows if the required linker is missing.
2. Projects with native dependencies
Some Rust crates depend on C or system libraries. In these cases, correct linker setup becomes even more important.
Examples include:
- database drivers
- graphics libraries
- cryptography libraries
- system integration crates
3. CI and build automation
In automated pipelines, builds fail if the machine has Rust installed but lacks the matching linker or SDK.
4. Team development on Windows
One developer may have Visual Studio Build Tools installed, while another only installed Rust. The second developer may hit link.exe not found immediately.
5. Cross-platform applications
Teams often need to understand which platform toolchain is used on each operating system:
- Linux: usually
gccorclang - macOS: usually Apple toolchain
- Windows MSVC:
link.exe - Windows GNU: GNU linker tools
Real Codebase Usage
In real projects, developers usually solve this in one of these ways.
Install and standardize the required toolchain
Teams often document setup steps such as:
- install Rust with
rustup - install Visual Studio Build Tools with C++ support
- verify with
rustup show - run
cargo build
This is common in onboarding guides and README files.
Use environment-specific guard checks
Build scripts and setup scripts often check whether expected tools exist before starting a full build.
For example, teams may verify:
- active target triple
- whether
link.exeis available - whether the Windows SDK is installed
Prefer consistent targets across a team
A project may standardize on one Windows target:
x86_64-pc-windows-msvcfor native Microsoft toolchain compatibilityx86_64-pc-windows-gnuif the team prefers GNU tools
Error diagnosis with verbose output
Developers often use:
cargo build -v
This helps confirm which linker Cargo is trying to invoke.
Configuration patterns
Some codebases include .cargo/config.toml settings for custom linkers or targets when the environment is not default. This is useful, but beginners should first make sure the standard linker setup works correctly.
Common Mistakes
1. Thinking the Rust code is wrong
Your Hello World program is valid.
Broken assumption:
fn main() {
println!("Hello, world!");
}
This code is not the problem.
How to avoid it
If the error mentions link.exe, gcc, ld, or another external tool, the issue is probably the build environment, not your syntax.
2. Installing Rust but not C++ build tools
Many beginners assume Rust includes every required Windows build tool.
How to avoid it
If using the MSVC target, install Visual Studio Build Tools with C++ components.
3. Not checking the active toolchain
You may think you are using GNU when you are actually using MSVC.
How to avoid it
Run:
rustup show
Check the active target carefully.
4. Installing tools but not reopening the terminal
Sometimes the tools are installed, but your current terminal session does not see updated environment variables.
How to avoid it
Close the terminal, open a new one, and retry cargo run.
5. Mixing toolchains without understanding them
For example, switching targets randomly can create confusion.
Potentially confusing commands:
rustup default stable-x86_64-pc-windows-msvc
rustup default stable-x86_64-pc-windows-gnu
How to avoid it
Choose one toolchain intentionally and make sure its matching linker tools are installed.
6. Assuming cargo clean will fix missing tools
cargo clean removes build artifacts, but it does not install link.exe.
cargo clean
cargo run
This will not solve a missing linker problem unless the environment issue was fixed separately.
Comparisons
MSVC vs GNU on Windows
| Toolchain | Typical target | Linker expected | Common use case | Notes |
|---|---|---|---|---|
| MSVC | x86_64-pc-windows-msvc | link.exe | Native Windows development | Requires Microsoft build tools |
| GNU | x86_64-pc-windows-gnu | GNU linker tools | Alternative Windows setup | Different compatibility trade-offs |
Compiler vs linker
| Tool | Job | Example in this problem |
|---|---|---|
| Compiler | Translates Rust source into lower-level compiled files | rustc compiles main.rs |
| Linker | Combines compiled files into final executable | link.exe creates helloworld.exe |
cargo build vs cargo run
| Command | What it does | Needs linker? |
|---|---|---|
cargo build | Builds the executable | Yes |
cargo run | Builds and then runs the executable | Yes |
Error source comparison
| Error type | Usually means |
|---|---|
| Syntax error | Your Rust code is invalid |
| Type error | Rust type rules are violated |
link.exe not found | Build toolchain is incomplete |
| Missing crate | Dependency configuration issue |
Cheat Sheet
Quick reference
Check active Rust toolchain
rustup show
Look for:
x86_64-pc-windows-msvc
If you see msvc, Rust expects Microsoft's linker.
Build or run
cargo build
cargo run
Verbose diagnostics
cargo build -v
Common cause of link.exe not found
- Rust MSVC toolchain is installed
- Visual C++ build tools are missing
- or terminal environment does not see them
Usual fix
Install Microsoft C++ build tools, including:
- MSVC compiler tools
- Windows SDK
- Desktop development with C++ workload
Alternative approach
Use GNU toolchain instead:
rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu
Key rule
If the error mentions the linker, your Rust source code may be perfectly correct.
Good troubleshooting order
- Run
rustup show - Check whether target is
msvc - Install required build tools
- Open a new terminal
- Run
cargo build -vagain
FAQ
Why does Rust need link.exe on Windows?
If you are using the MSVC target, Rust relies on Microsoft's linker to create the final executable.
Is my Hello World code wrong?
No. The code is valid. The problem is the missing linker tool, not the Rust source.
How do I know whether I am using MSVC or GNU?
Run rustup show. The target triple will include either msvc or gnu.
What should I install to fix link.exe not found?
Install Visual Studio Build Tools or Visual Studio with the C++ workload and Windows SDK.
Can I avoid link.exe completely?
Yes, by using the GNU Rust target on Windows. However, that changes the toolchain and may affect compatibility.
Why did Rust install successfully if building still fails?
Rust itself installed correctly, but the selected Windows toolchain also depends on external system build tools.
Does cargo clean fix this error?
No. cargo clean removes build artifacts, but it does not install a missing linker.
Should I reinstall Rust?
Usually not. First verify your toolchain and install the matching linker tools.
Mini Project
Description
Create a small Rust environment checker for Windows that helps confirm whether your Rust setup is using the MSVC toolchain and whether common build commands work. This project is useful because it teaches you to diagnose toolchain problems instead of guessing whether the Rust code is broken.
Goal
Build a simple command-line program and verification workflow that confirms your active Rust target and helps you test whether your system can compile a Rust executable successfully.
Requirements
- Create a new Rust binary project with Cargo.
- Print a short message from
mainso you can verify the executable runs. - Use
rustup showoutside the program to identify whether your active toolchain ismsvcorgnu. - Run
cargo buildorcargo runand observe whether linking succeeds. - If you are using
msvc, verify that the required Microsoft C++ build tools are installed.
Keep learning
Related questions
Absolute Values in Rust: Using the abs() Method
Learn how to calculate absolute values in modern Rust with abs(), handle signed integer overflow, and choose safer alternatives.
Accepting Iterators in Rust Functions with impl Iterator
Learn how to write Rust functions that accept HashMap iterators using generic bounds or impl Iterator, avoiding trait object and lifetime errors.
Access Enum Variant Values in Rust with Pattern Matching
Learn how to extract and print values stored in Rust enum variants using match and if let pattern matching, with practical examples.