无法在Windows上编译Rust hello world:找不到链接器link.exe

Zob*_*wal 18 installation compiler-errors linker-errors build-tools rust

我已经从Rust安装页面在Windows上安装了Rust 。安装后,我尝试运行“ hello world”程序,但收到以下错误。

>cargo run
Run Code Online (Sandbox Code Playgroud)

错误

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.
Run Code Online (Sandbox Code Playgroud)

代码:

fn main() {
    println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)

Zob*_*wal 32

我下载并安装了Visual Studio 2019的构建工具。在安装过程中,我选择了C ++工具。它下载了将近5GB的数据。我在安装后重新启动计算机,并编译了代码,效果很好:

> 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!
Run Code Online (Sandbox Code Playgroud)

  • @argenkiwi 我能够仅使用“MSVC v142 - VS 2019 C++ x64/x86 构建工具”和“Windows 10 SDK”来使其工作。 (8认同)
  • 我们需要选中所有默认复选框吗?否则,为了让 Rust 工作,我们可以安装的最小依赖集是什么? (5认同)
  • 我只尝试了 C++ 构建工具,但它不起作用 - 您还需要 Windows 10 SDK。 (2认同)

Rah*_*ali 30

好的!

这正是我所做的。转到https://visualstudio.microsoft.com/visual-cpp-build-tools/,它将下载 Visual Studio 安装程序。

以管理员身份运行它,然后确保下载了下面屏幕截图中列出的所有三个内容,版本并不重要,只需尝试获取最新版本即可。

所需清单

点击安装。重新启动计算机。不客气。

  • 在“工作负载”选项卡中,单击“使用 C++ 进行桌面开发”,然后您将在右侧窗格中看到上面的列表。 (3认同)

oca*_*nis 25

案例 1:使用 C++ win 编译器,要修复它,您需要重新安装 VS 构建工具 C++ 从 Microsoft 网站下载 Visual Studio 2019 构建工具:https : //visualstudio.microsoft.com/thank-you-downloading-visual-studio /?sku=BuildTools&rel=16

下载后,在安装构建工具时,请确保安装所需的组件:

  1. C++ 构建工具

这将下载所需的文件。一旦一切都成功安装,重启并重新运行你的 Rust 程序,它将成功编译。

Case2:这个错误可能来自于你使用 GCC 编译、修复它(假设你已经有了 MinGW):

cmd 中的磁带:

  1. rustup uninstall toolchain stable-x86_64-pc-windows-msvc
  2. rustup toolchain install stable-x86_64-pc-windows-gnu (或在https://forge.rust-lang.org/infra/other-installation-methods.html下载您选择的平台的 rustup-init )
  3. rustup default stable-x86_64-pc-windows-gnu

案例3:你不想下载带有构建工具的Visual Studio,只需安装带有g++ gcc开发包的MinGw,然后运行CASE 2

  • @dkellner 也许我写的时候没有戴眼镜,兄弟! (4认同)

小智 21

我有一个类似的问题“ error: linking with link.exe failed: exit code: 1”

为了解决它,我做了

rustup toolchain install stable-x86_64-pc-windows-gnu
Run Code Online (Sandbox Code Playgroud)

然后

rustup default stable-x86_64-pc-windows-gnu
Run Code Online (Sandbox Code Playgroud)

和

cargo build
  Compiling hello v0.1.0 (C:\Users\leke\dev\rust\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.66s
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个解决方案,而是一种解决方法,重要的是要考虑在执行此操作时更改工具链并且会产生副作用。 (14认同)
  • @Hashimaziz 提议的解决方案改变了工具链,从 MSVC 到 GNU。它没有解决原来的问题(可能是 MSVC 安装错误),而是使用完全不同的编译器和链接器。兼容性、功能和性能可能不相同。为了更好地理解其中的差异,请了解将 Windows 开发从 MSVC 更改为 GNU 的影响。 (7认同)
  • @LuisAyuso 如果您列出您正在考虑的副作用,您的评论将会很有帮助。 (5认同)

Adm*_*tor 21

如果上述解决方案仍然不适合您(这是 2021 年),Rust 使用msvc和gnu编译器,因此您可以随时切换到 gnu 编译器:

$ rustup default stable-x86_64-pc-windows-gnu
Run Code Online (Sandbox Code Playgroud)

  • 我的电脑磁盘空间严重不足(即我无法安装 C++ 构建工具,因为它有 6 GB),你救了我,谢谢你,好心的陌生人:) (2认同)

fil*_*gan 6

错误信息很不清楚,因为不需要安装 Visual Studio 来运行 Rust 代码。“Visual C++ 选项”究竟是什么意思?“VS 2013、VS 2015 或 VS 2017”也是错误的 - 无需安装这些特定版本的完整 Visual Studio。

要执行“cargo run”,您需要从Build Tools for Visual Studio安装C++ 构建工具。2019 版就好了。下载链接:https : //visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16#

重要的是不仅要在安装过程中选择默认的“包含”C++ 工具,还要选择三个“可选”的 C++ 构建工具:MSVC(...)、Windows 10 SDK、C++ CMake 工具适用于 Windows。

  • @kmdreko,答案并没有解决问题,因为那里没有提到 MSVC 和 Windows 10 SDK 等其他要求 (5认同)

Mar*_*vic 6

在 PowerShell 终端中运行:

winget install -e --id Microsoft.VisualStudio.2022.BuildTools
Run Code Online (Sandbox Code Playgroud)

然后:

  • 打开Visual Studio 安装程序
  • 按修改
  • 检查使用 C++ 进行桌面开发
  • 按修改