Rust 第一课; Windows 环境报错:error: linker `link.exe` not found

259 阅读2分钟

Windows 环境下运行 Rust语言编写的 Hello World.在安装配置rust完成后,执行rustc main.rs 出现了以下错误:

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.

note: VS Code is a different product, and is not sufficient.

error: aborting due to previous error

错误的第三句话note: the msvc targets depend on the msvc linker but link.exe was not found就告诉我们:没有办法链接到 msvc 。 查询得知 msvc 是Windows环境用来模拟Linux环境中gcc提供的。 msvc 的全城是 MinGW(Minimalist GNU for Windows)。

Rust官网提示说下载 VScode就可以了,但是我下载了不管用。找到了第二种方法,运行一下命令:

 rustup toolchain install stable-x86_64-pc-windows-gnu
 rustup default stable-x86_64-pc-windows-gnu

第一句:用来安装稳定版本的 Rust GNU 工具链。其中:- stable:表示稳定版本

  • x86_64:表示 64位
  • pc-windows-gnu:表示用于 Windows 的 GNU 工具链加起来就是安装最新稳定版本的 64位 Rust GNU 工具链给 Windows 使用。

第二局:用于设置默认的 Rust 工具链。也就是说后面使用 rustccargo 等命令时,会默认使用这个刚刚安装的 Rust GNU 工具链。综上,这两条命令的作用就是:1. 为 Windows 安装稳定版本的 64位 Rust GNU 工具链
2. 设置该工具链为默认,用于后续的 Rust 开发使用 GNU 工具链可以避免 Rust 与 Visual Studio 的兼容性问题,提供更纯粹的跨平台开发体验。所以在 Windows 上通常会选择使用 Rust GNU 工具链。这两条命令就是完成该工具链的安装和配置

安装完毕再次运行:输出 Hello World