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

Unable to compile Rust hello world on Windows: linker link.exe not found

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

1
>cargo run

错误

1
2
3
4
5
6
7
8
9
10
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.

码:

1
2
3
fn main() {
    println!("Hello, world!");
}


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

1
2
3
4
5
> 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!


情况1:使用C ++ win编译器进行修复,需要重新安装VS构建工具C ++

从Microsoft网站下载Visual Studio 2019 Build工具:
https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16

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

  • C ++构建工具
  • 这将下载所需的文件。一旦一切都成功安装,重新启动并重新运行您的rust程序,它将成功编译。
    情况2:此错误可能来自您使用GCC进行编译,修复(假设您已经拥有MinGW)的事实:
    胶带(cmd):

  • rustup uninstall toolchain stable-x86_64-pc-windows-msvc
  • rustup toolchain install stable-x86_64-pc-windows-gnu
    (or download rustup-init for the platform of your choice at https://forge.rust-lang.org/infra/other-installation-methods.html)
  • rustup default stable-x86_64-pc-windows-gnu
  • 情况3:您不想下载带有构建工具的Visual Studio,只需将MinGw与g ++ gcc开发包一起安装,然后运行CASE 2


    我遇到了同样的问题,并且发现即使在安装了构建工具后,它仍然存在。我几乎是偶然地意识到我正在" Visual Studio开发人员命令提示符"中运行所有货物命令。在简单的cmd shell中运行相同的命令不会出现任何问题。

    对我有用的方法:直接运行命令提示符,而不使用Visual Studio创建的快捷方式。

    可能的原因:Visual Studio命令提示符运行bat文件,例如VsDevCmd.bat在启动外壳之前(以加载VS相关的环境变量等),并且该文件中的其中一个命令可能会弄乱货物用来到达链接器的路径。

    如果他们真的想知道,则可以进一步挖掘以找到导致问题的确切路线。


    我有类似的问题" error: linking with link.exe failed: exit code: 1"

    为了解决它,我做了

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

    然后

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

    1
    2
    3
    4
    cargo build
      Compiling hello v0.1.0 (C:\\Users\\leke\\dev\
    ust\\hello)
        Finished dev [unoptimized + debuginfo] target(s) in 1.66s


    请尝试在Visual Studio之外使用Powershell。

    然后,货物在src的父文件夹中运行。

    您也可以尝试:rustc

    祝好运。