Windows下Rust开发遇link.exe not found错误,根源是MSVC工具链缺失链接器;解决方案为安装Microsoft C++ Build Tools并勾选“Desktop development with C++”工作负载,或切换至GNU工具链(x86_64-pc-windows-gnu);rust-analyzer卡在“Loading…”需手动执行rustup component add rust-src。

VS Code 能跑 Rust,但直接装完插件就写代码,90% 的人会在 link.exe not found 或 rust-analyzer 不工作上卡住——问题不在 Rust 本身,而在工具链和 VS Code 的协同配置没对齐。
Windows 下 rustc 能运行但 cargo build 失败:缺 linker
现象是执行 cargo build 报错:linker `link.exe` not found,或提示 failed to run custom build command for ...。这不是 Rust 安装失败,而是 Windows 默认不带链接器。
- 必须安装 Microsoft C++ Build Tools(不是完整 Visual Studio),选 “Desktop development with C++” 工作负载
- 安装后要**重启终端**,或用 “Developer Command Prompt for VS 2022” 启动命令行,否则环境变量不生效
- 验证方式:在开发者命令提示符里运行
link.exe /?,有帮助输出才算到位 - 如果坚持用 MinGW,安装时 rustup-init 选 2(自定义),把 host triple 改成
x86_64-pc-windows-gnu,并确保gcc和ld在 PATH 中
rust-analyzer 启动后一直显示 “Loading…”
常见于首次打开项目、或刚装完插件时,表面是卡住,实则是 rust-analyzer 在后台下载 rust-src 组件或索引依赖。
- 手动触发下载:
rustup component add rust-src(必需)和rustup component add rust-analysis(可选,增强分析) - 检查 VS Code 设置中是否误启用了旧版
Rust插件(IDrust-lang.rust),它与rust-analyzer冲突,必须禁用 - 若项目含大量 proc-macro(如
serde,tokio),需在settings.json中启用:"rust-analyzer.procMacro.enable": true - macOS/Linux 用户遇到此问题,大概率是 shell 初始化文件(如
~/.zshrc)没 source$HOME/.cargo/env,导致 rust-analyzer 找不到cargo
调试时 launch.json 配置失败:找不到可执行文件
点绿色 ▶️ 运行调试,报错 Cannot find program '.../target/debug/demo',本质是构建未触发或路径写死。
- 不要手动写死
program路径,用 VS Code 变量:"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}" - 确保
tasks.json存在且 label 为build,并在launch.json中通过"preLaunchTask": "build"关联 - Windows 用户注意:
type必须是lldb(CodeLLDB 插件提供),不是cppvsdbg;且sourceLanguages设为["rust"],否则断点不命中 - 第一次调试前,先手动跑一次
cargo build,确认target/debug/下生成了可执行文件
macOS / Linux 用户跳过 C 工具链安装?别信
虽然文档说“macOS 自带 clang”,但实际常因 Xcode 命令行工具未安装或版本过旧,导致 cargo build 卡在 linking 阶段,报错类似 ld: library not found for -lc。
- macOS 必做:
xcode-select --install,再运行sudo xcode-select --switch /Library/Developer/CommandLineTools - Linux(如 Ubuntu)需确保安装了
build-essential(含gcc,g++,make),仅装gcc不够 - 所有平台都建议运行
cargo check测试编译器前端是否通,再跑cargo build测试整个工具链
最容易被忽略的点:VS Code 打开项目时默认不信任工作区,rust-analyzer 和调试器会降级运行。右下角弹出“Trust this workspace?”时,务必点 “Trust” —— 否则补全延迟、断点失效、甚至 Cargo.toml 里的依赖不解析。


















