问题描述

在 greeting 项目目录下执行 cargo build 时报错:

1
2
3
4
5
6
7
8
9
error: linker `link.exe` not found
|
= note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.

error: could not compile `greeting` (bin "greeting") due to 1 previous error

原因分析

  • Rust 在 Windows 上默认使用 MSVC 工具链(stable-x86_64-pc-windows-msvc)
  • MSVC 工具链依赖 Visual Studio 的 C++ 生成工具(Build Tools)提供的 link.exe 链接器
  • 本机未安装 Visual Studio 或 Build Tools,因此链接器缺失,编译无法完成

确认当前工具链:

1
rustup show

输出显示默认工具链为 stable-x86_64-pc-windows-msvc,且系统中没有安装带 C++ 组件的 Visual Studio。

解决方案

方案选择:切换到 GNU 工具链(stable-x86_64-pc-windows-gnu)。

  • 自带 MinGW 链接器,无需安装 Visual Studio
  • 不需要管理员权限
  • 体积小、安装快(MSVC 方案需下载约 2~4 GB 的 VS Build Tools)

步骤 1:安装 GNU 工具链(使用国内镜像)

直接安装会因连接 static.rust-lang.org 超时失败(os error 10060),需先设置字节跳动镜像源(rsproxy.cn)再安装:

1
2
3
$env:RUSTUP_DIST_SERVER = 'https://rsproxy.cn'
$env:RUSTUP_UPDATE_ROOT = 'https://rsproxy.cn/rustup'
rustup toolchain install stable-x86_64-pc-windows-gnu

安装成功后输出:

1
stable-x86_64-pc-windows-gnu installed - rustc 1.99.1 (58cf8f9c3 2026-10-13)

步骤 2:设为默认工具链

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

步骤 3:重新构建验证

1
cargo build

构建成功:

1
2
Compiling greeting v0.1.0 (D:\backend_workpalce\test_rust\greeting)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 15.02s

备选方案(未采用):安装 MSVC 生成工具

如果希望继续使用 MSVC 工具链(Windows 上更推荐用于正式发布),可以安装 Visual Studio 生成工具:

  1. 下载 Visual Studio Build Tools
  2. 安装时勾选 “使用 C++ 的桌面开发” 工作负载
  3. 安装完成后重新打开终端,执行 cargo build 即可